2013-11-24 161 views
0

由于某种原因,我的表单在第一次“提交”时提交一次,然后在第二次提交时提交两次,如果不提交刷新两者之间的页面。ajax表格在第二次提交时提交两次,第三次提交时提交3次等

$(document).on('submit', ".post-video-form", function(uploadVideo) { 
    uploadVideo.preventDefault(); 
    var formData = new FormData($(this)[0]); 
    $.ajax({ 
     type: 'post', 
     url: '/dev/new/scripts/upload_video.php', 
     data: formData, 
     success: function (response) { 
      if (response == 'success') { 
       $(".overlay").fadeOut(200); 
       setTimeout(function() { 
        $(".overlay").remove(); 
       }, 250); 
       setTimeout(function() { 
        $(".head").before(overlay_success_html); 
        $(".overlay").fadeIn(); 
       }, 255); 
       $(document).on('click', ".close-overlay", function(){ 
        $(".overlay").fadeOut(200); 
        setTimeout(function() { 
         $(".overlay").remove(); 
        }, 250); 

       }); 
      } 

      if (response == 'failed') { 
       $(".upload-error").append('Upload error: File must be a JPG or PNG less than 2.0MB'); 
      } 
     }, 
     error: function() { 
      $(".upload-error").append('Upload error: File must be a JPG or PNG less than 2.0MB'); 
     }, 
     cache: false, 
     contentType: false, 
     processData: false 
    }); 
}); 

回答

0

需要查看更多的代码,但是可能您的提交处理程序正在为每个发布的文章额外注册一次。

尝试之前

$(document).on('submit', ".post-video-form", function(uploadVideo) { 

添加就行了控制台日志或警报,以确保提交事件未被注册多次。如果你正在生成JavaScript服务器端,那么这可能是这种情况。否则,我们需要更多信息。

相关问题