2017-02-28 145 views
0

我想在表單提交時執行一些操作。但首先我想檢查表單是否應該刪除任何東西。事情是,我不能讓我的表單再次提交。我該如何解決?防止表單提交到SweetAlert爲真

$('body').on('submit', 'form', function (e) 
     { 
      let self = $(this); 
      if (self.find('input[name="_method"]').val() == 'DELETE') 
      { 
       e.preventDefault(); 
       swal({ 
         title: window.translations.delete_item_title, 
         text: window.translations.delete_item_content, 
         type: "warning", 
         showCancelButton: true, 
         confirmButtonColor: "#DD6B55", 
         confirmButtonText: window.translations.delete_item_confirm, 
         cancelButtonText: window.translations.delete_item_cancel, 
         closeOnConfirm: false 
        }, 
        function (input) 
        { 
         if(input){ 
          $(this).submit(); 
         } 
        }); 
      } 
     }); 

回答

0

在你SweetAlert功能齊全this將這個函數內的作用域,而不是原始提交功能。當您提交,你正在創建一個變量稱爲自 - 你需要調用.submit()方法對這個靠近你的代碼的末尾:

if(input){ 
    self.submit(); 
} 

此外,我個人建議是私有成員,如self變量給出下劃線前綴; _self。這消除了程度模糊。