Ajaxsubmitbutton Not Passing ID
Ajaxsubmitbutton Not Passing ID
我在使用引导模式更新数据时遇到问题。
在CGridView selectionChanged上,它将触发一个函数来显示模式对话框表单并将数据插入表单
这是我的CGridView:
<?php
$this->widget('customCGridView', array(
'id'=>'contactperson-grid',
'itemsCssClass' => 'table table-bordered',
'selectionChanged'=>'showmodal',
'dataProvider' => $contactperson->search(),
'emptyText' => '',
'enableSorting' => false,
'htmlOptions' => array('class' => ' '),
'columns' => array('nama', 'jabatan')
));
?>
在selectionChanged时,它将触发此脚本打开更新对话框模式,并用选定的CGridView列数据填充表单:
function showmodal(grid_id) {
var keyId = $.fn.yiiGridView.getSelection(grid_id);
keyId = keyId[0]; //above function returns an array with single item, so get the value of the first item
$.ajax({
url: '<?php echo $this->createUrl("suppliertable/personview"); ?>',
data: {id: keyId},
type: 'GET',
success: function(data) {
$("#contact-person-modal").html(data);
$('#kontakmodalupdate').modal('show');
}
});
}
以下是actionPersonView,它将在选择列时执行:
public function actionPersonView($id) {
$contactperson = SupplierContactPersonTable::model()->findByPk($id);
if ($contactperson === null)
throw new CHttpException(404, 'The requested page does not exist.');
$this->renderPartial('updateForm', array('contactperson'=> $contactperson));
Yii::app()->end();
}
现在,问题来了:
当我点击更新表单上的更新按钮时,我需要将ID传递给我的操作:
public function actionPersonUpdate($id) {
$model2=$this->loadModel2($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['SupplierContactPersonTable']))
{
if($model2 === null) {
throw new CHttpException(404, 'The requested page does not exist.');
}
$model2->attributes=$_POST['SupplierContactPersonTable'];
$model2->save();
}
}
为了传递这个值,我在表单上使用AjaxButton:
<div id="contact-person-modal">
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'suppliercontactmodalform',
'htmlOptions' => array(
'class' => 'smart-form',
'novalidate' => '1',),
'enableAjaxValidation' => false,));
?>
...
<footer>
<button type="button" class="btn btn-labeled btn-danger cancel-button" style="float: left">
Hapus Pengguna
</button>
<?php
echo CHtml::ajaxButton('Update', Yii::app()->createUrl('suppliertable/personupdate',array('id'=>$contactperson->contact_person_id)), array('type'=>'POST'), array('type'=>'submit',"class" => "btn btn-primary"));
?>
<button type="button" class="btn btn-default" data-dismiss="modal">
Batal
</button>
</footer>
...
然而,ID似乎没有通过。这是Firebug控制台:
POST http://localhost/webapp/index.php/suppliertable/personupdate/
400 Bad Request
正如您所看到的,没有传递任何ID。
但是,如果我使用静态参数值:
echo CHtml::ajaxButton('Update', Yii::app()->createUrl('suppliertable/personupdate',array('id'=>'5')), array('type'=>'POST'), array('type'=>'submit',"class" => "btn btn-primary"));
ID已传递:
POST http://localhost/webapp/index.php/suppliertable/personupdate/5
所以,我认为问题就在这里,不是输出值:
$contactperson->contact_person_id
这里是从Yii:生成的jQuery
jQuery('body').on('click','#yt0',function({
jQuery.ajax({
'type':'POST',
'url':'/webapp/index.php/suppliertable/personupdate id=',
'cache':false,
'data':jQuery(this).parents("form").serialize()
});
return false;
});
感谢您的帮助:)
我找到了我的解决方案。
如果有人有同样的问题,我需要更新我的actionPersonView方法:
public function actionPersonView($id) {
if (Yii::app()->request->isAjaxRequest) {
Yii::app()->clientScript->scriptMap['jquery.js'] = false;
Yii::app()->clientScript->scriptMap['jquery.min.js'] = false;
$contactperson = SupplierContactPersonTable::model()->findByPk($id);
if ($contactperson === null)
throw new CHttpException(404, 'The requested page does not exist.');
$this->renderPartial('updateForm', array('contactperson'=> $contactperson),false,true);
Yii::app()->end();
}
}
我需要在我的actionPersonView方法中添加
$this->renderPartial('updateForm', array('contactperson'=> $contactperson),false,true);
并添加
Yii::app()->clientScript->scriptMap['jquery.js'] = false;
Yii::app()->clientScript->scriptMap['jquery.min.js'] = false;
以防止脚本被多次执行。
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- difference between '$(<%= DDL.ID %>) & $('
- 使用Javascript获取所选选项ID
- 根据id将json数组组合为一个json数组
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 对id以某个字符串开头的元素进行计数
- 如果元素's的ID以数字开头
- 通过id和class属性获取元素
- 根据选项卡内部的文本链接中的哈希ID动态选择jqueryUI选项卡
- 使用当前日期生成随机id
- Javascript-ID冲突的几率
- Href:当前DIV中的目标ID
- 无法获取属性'Id'使用Knockout.js的未定义或空引用API
- Javascript复选框函数:;缺少:在属性id之后"
- Twitter引导程序Typeahead-Id&标签
- 如何与特定ID交谈
- chart.series[id].remove()无法刷新高位图表/高位股票中其他系列的图例属性
- 如何在速度模板中获取LiferayPortlet实例id
- 谷歌地图可以't添加正确的标记ID
- Ajaxsubmitbutton Not Passing ID