2017-05-19 162 views
0

我在一頁上有2個表單。一個上傳圖片和其他上傳規格。JQuery提交表單多次/

問題 我面臨與發送的形式要求多次我的Ajax請求的問題。 如果點擊「上傳圖片」,它會像幾次一樣上傳相同的圖片。它會減少點擊事件並將其添加到每次新事件。

我試着用.unbind()來修復這個;它的工作原理但是,如果我點擊上傳規格,它會運行上傳圖片請求。所以.Unbind()沒有工作。

我該如何解決這個問題。所以正確的ajax請求只發送一次。

這是我的圖片上傳代碼。

//IMAGE UPLOAD 
    $(document).unbind('submit').bind('submit', "#UploadImage",function() { 

     event.preventDefault(); 

     var customerId = $(this).parent().find('input[name="customerId"]').val(); 
     var image = $(this).parent().find('input[name="image"]').val(); 

     //send ajax request 
     jQuery.ajax({ 
     url: "../data/stock.php?action=stock-image-upload", 
     type: "POST", 
     data: new FormData(this), 
     processData: false, //prevent jQuery from converting your FormData into a string 
     contentType: false, 
     success: function(data, textStatus, jqXHR) { 
     console.log(2); 
      $('.image-viewer-modal').modal('hide'); 
      var filter = "<?php echo $id_stc ?>"; 
      var tab_content_to_change = "#stock-sub-list"; 
      jQuery(tab_content_to_change).load('/tasks/stock/stock-list.php?filter='+filter); 


     }, 
      error: function(jqXHR, textStatus, errorThrown){ 
      //Display error message to user 
      alert("An error occured when saving the data"); 
     } 
    }); 
}); 

這裏是規範上傳。

//specification UPLOAD 
    $(document).unbind('submit').bind('submit', "#Uploadspecification ",function() { 

     event.preventDefault(); 

     var customerId = $(this).parent().find('input[name="customerId"]').val(); 
     var specification = $(this).parent().find('input[name="specification "]').val(); 

     //send ajax request 
     jQuery.ajax({ 
     url: "../data/stock.php?action=stock-specification -upload", 
     type: "POST", 
     data: new FormData(this), 
     processData: false, //prevent jQuery from converting your FormData into a string 
     contentType: false, 
     success: function(data, textStatus, jqXHR) { 
     console.log(2); 
      $('.image-viewer-modal').modal('hide'); 
      var filter = "<?php echo $id_stc ?>"; 
      var tab_content_to_change = "#stock-sub-list"; 
      jQuery(tab_content_to_change).load('/tasks/stock/stock-list.php?filter='+filter); 


     }, 
      error: function(jqXHR, textStatus, errorThrown){ 
      //Display error message to user 
      alert("An error occured when saving the data"); 
     } 
    }); 
}); 

我怎樣才能讓我點擊上傳按鈕,每次清除提交事件,對Ajax請求事先只發送一次

非常感謝。

回答

0

嘗試與異步:假 或另一端,你可以點擊點擊後沒有一個事件的一次

+0

我只是去嘗試,它仍然發出多個請求。 –