Yii2在提交表单之前使用渲染失败,但使用renderajax

时间:2016-12-11 12:00:21

标签: php jquery ajax forms yii2

当我在我的控制器中使用渲染时,我在调用之前有一个表单,表单在没有使用默认提交的ajax提交的情况下继续进行,但在使用renderAjax的模式形式时工作

是控制器代码:

public function actionProfile()
{
 if(\Yii::$app->request->post()){

   //perform acctions
}else{
       return $this->render('profile',["user" =>$user]); //this works when its renderAjax
    }

在from

div class="panel-body">
     <?php $form = ActiveForm::begin([ 'id' => "userform",]); ?>
       // form fields

       <?= Html::submitButton('Update Profile')  ?>  //its an update
     <?php ActiveForm::end(); ?>

现在表单视图上的js脚本

<?php
$script = <<< JS

   $('form#userform').on('beforeSubmit', function(e) {

    e.preventDefault() //tried adding this but also fails
    // do stuff here

     return false;
  })

 JS;
 $this->registerJs($script,   \yii\web\View::POS_LOAD);  //thus its on document.ready

&GT;

此表单提交就像普通表单一样,不通过ajax

完成

我不能再使用渲染Ajax,因为应用程序的设计从模态改为平面设计,但我需要通过ajax提交以触发通知并防止页面刷新。

0 个答案:

没有答案