【发布时间】:2016-03-16 18:31:01
【问题描述】:
在我的 Yii Web 应用程序中,任何类型的 Ajax 调用,如 Ajax 验证、Ajax 依赖下拉列表等......都不起作用。
我的代码是, 在我的表单页面中:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'workdetails-form',
'enableClientValidation' => true,
'clientOptions' => array(
'validateOnChange' => true,
'validateOnSubmit' => true,
),
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation' => true,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
));
?>
在控制器中:
public function actionCreate() {
$model = new Workdetails;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if (isset($_POST['Workdetails'])) {
$model->attributes = $_POST['Workdetails'];
if ($model->validate()) {
if ($model->save()) {
$this->redirect(array('create'));
}
}
}
$this->render('create', array(
'model' => $model,
));
}
对于依赖下拉:
<div class="form-group col-sm-6">
<?php echo $form->label($model, 'designationid', array('class' => 'req')); ?>
<?php
$designation = CHtml::listData(Designation::model()->findAll(), 'designationid', 'designation_name');
echo $form->dropDownList($model, 'designationid', $designation, array(
'class' => 'form-control',
'prompt' => 'Please Select',
'ajax' => array(
'type' => 'POST',
'url' => $this->createUrl('workdetails/Fetchemployee'), // here for a specific item, there should be different URL
'update' => '#' . CHtml::activeId($model, 'employeeid'), // here for a specific item, there should be different update
'data'=>array('designationid'=>'js:this.value'),
)));
?>
<?php echo $form->error($model, 'designationid', array('class' => 'school_val_error')); ?>
</div>
如何解决这个问题... 请帮帮我..
【问题讨论】:
-
调试一下怎么样。我会先检查ajax调用是否通过。打开开发者控制台并检查网络选项卡。如果它不知道你的问题在哪里。然后将 var_dump($model) 放入您的 performAjaxValidation 函数中,看看会发生什么。
-
在开发者控制台中,没有显示任何东西。对于依赖下拉,同时,下拉改变了整个页面被重新加载。这就是我说 Ajax 不起作用的原因。