2016-02-11 86 views
1

我想通过pjax提交表单,提交后我想刷新表格,但在提交表单并尝试重新加载($.pjax.reload({container:'#products-table'});)后,jquery脚本不再工作。即使我添加了pjax:success,但是如果我不重新加载表格,所有脚本都在工作并发送请求,然后我必须手动重新加载以查看更改。或者我已经尝试提交没有pjax的表单,页面由Yii重新加载,然后再次使用脚本。请您的反馈。由于Yii2 pjax reload

$(document).on('ready pjax:success', function(){ 
    $("form.products-input-style").on('beforeSubmit', function(e){ 

      var form = $(this); 

      $.ajax({ 
       type : "POST", 
       url: form.attr('action'), 
       data: new FormData(this), 
       processData: false, 
       contentType: false, 
       success : function(response) { 
        $.pjax.reload({container:'#products-table'}); 
        $.notify({ 
         icon: "pe-7s-check", 
         message: "Product is updated." 

        },{ 
         type: type[2], 
         timer: 10, 
         placement: { 
          from: 'top', 
          align: 'right' 
         } 
        }); 
       }, 
       error : function(response){ 
        console.log(response); 
        $.notify({ 
         icon: "pe-7s-close-circle", 
         message: "Error! " + response 

        },{ 
         type: type[4], 
         timer: 10, 
         placement: { 
          from: 'top', 
          align: 'right' 
         } 
        }); 
       } 
      }); 

      return false; 

     }).on('submit', function(e){ 
      e.preventDefault(); 
     }); 
}); 

回答

0

如果您有pjax重装后输入形式的问题,那么,尝试禁用警予客户端验证:

ActiveForm::begin([ 
    'enableClientScript' => false, 
]); 

这是警予一些bug \部件\的ActiveForm和警予\引导\的ActiveForm。

0

我认为你的处理程序将被新的html代码所替代(在Pjax更新之后)。 尝试使用delegated handlers

例如:

$(function(){ 

    $(document).on('beforeSubmit', 'form.products-input-style', function(event){ 
     // ... 
    } 

});