2016-02-11 82 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){ 
     // ... 
    } 

});