Q : 如何通过ajax 将数据值添加到表格行的标签中?
状态:在表格中添加新行正在工作。此时,当用户选择商品时,价格应显示在标签(价格)处。从控制器返回很好。但价格没有显示在标签(价格)上。
这是我的看法
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'acc-recei-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<?php
Yii::app()->clientScript->registerScript('add-item', "
$('#add-item').click(function(){
//console.log(item_template);
$('#template .template_item').clone().appendTo('#item_list');
$('#item_list .template_item').fadeIn();
return false;
});
$('#AccRecei_acc_category_id').live('change', function(){
var itemID = $(this).val(),
vNode = $(this);
$.ajax({
url: 'getitem/'+itemID,
dataType: 'json',
success: function(data){
var target = $(vNode).parents('tr');
alert(target);
$(target).find('.price').val(data.item_price);
}
});
//alert(vNode);
});
");
?>
<table id="item_list">
<tr>
<td>name</td>
<td>price</td>
<td>qty</td>
<td>row_total</td>
<tr>
</table>
<!--- other fields -->
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<table id="template" style="display:none;" >
<tr class="template_item">
<td><?php
echo $form->dropDownList($model,'acc_category_id', $accitemslist, array(
'prompt'=>'Please select a category',
'options'=>array("$model->acc_category_id"=>array('selected'=>'selected')),
)
);
?></td>
<td><label id="price" class="price"></label></td>
<td><input type="text" name="item_qty[]" class=".qty" /></td>
<td><label id="row_total" class="row_total[]"></label></td>
<tr>
</table>
这是控制器(来自 ajax 的调用)
public function actionGetitem()
{
echo CJSON::encode(array(
'item_price'=>12,
));
}