【问题标题】:Ajax is not working in yiiAjax 在 yii 中不起作用
【发布时间】: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 不起作用的原因。

标签: ajax yii


【解决方案1】:

Arya 我在使用 Yii1 时遇到了同样的问题,我放弃了使用 yii-ajax 验证,因为我找不到修复它的方法。首先确保你有初始化/注册 Yii-js 文件这些是

yiiactiveform 和 yii.js

如果您的项目中没有这些文件,则表示您尚未注册它们。要注册核心 JS 文件,请在您的 main 中继续此配置。

'clientScript' => array(
            'scriptMap' => array(
                'jquery.js' => true,
                'jquery.min.js' => true,
            ),
        ),

或者如果这不起作用,请在标题部分的主视图中使用它。

 Yii::app()->clientScript->registerCoreScript('jquery');

您也可以将其添加到位于 components/Controller.php 的基本控制器中

public function init() {
     parent::init();
    Yii::app()->clientScript->registerCoreScript('jquery');
}

在您创建表单时,您认为有这个。它将有助于放置错误消息。到你的元素

<?php
            $form = $this->beginWidget('CActiveForm', array(
                'id' => 'patient-registration-form',
                'enableClientValidation' => True,
                'enableAjaxValidation' => FALSE,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                    'afterValidate' => 'js:function(form, data, hasError) {
                  if(hasError) {
                      for(var i in data) $("#"+i).parent().addClass("has-error");
                      return false;
                  }
                  else {
                      form.children().removeClass("has-error");
                      return true;
                  }
              }',
                    'afterValidateAttribute' => 'js:function(form, attribute, data, hasError) {

                  if(hasError) $("#"+attribute.id).parent().addClass("has-error");
                      else $("#"+attribute.id).parent().removeClass("has-error");
                      $("#"+attribute.id).parent().addClass("has-success");
              }'
                ),
                'htmlOptions' => array(
                    'class' => 'form-horizontal form-bordered form-row-stripped',
                ),
            ));
            ?>

或者使用 Yii2,它已经修复了很多东西,如果你使用 ajax 加载当前页面,你需要再次渲染整个页面,包括 js 文件。因为当您使用 renderPartial 时,它不会初始化 js 文件,因此没有 js 脚本可以工作,包括验证。

【讨论】:

  • 通过使用此代码,我收到了类似 ReferenceError: jQuery is not defined 的错误。我试图解决这个错误但失败了。
  • 如果它说你还没有定义 jQuery( 容易检查:去你的视图文件做一个脚本标签并在 $(document).ready(function(){ alert("Something !”);}))。如果它在页面重新加载后弹出,则它被定义,否则你根本没有定义它。另一个可能的问题是你定义了它两次,所以 grep 或者检查你的 views/layout/main.php 文件或你正在渲染这个表单的文件,看看它是否被声明。我遇到过很多我忘记定义 jQuery 的例子......每次都面对手掌时刻。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
  • 2014-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多