ajax表单在第二次提交时提交了两次,在第三次提交时又提交了三次,等等

ajax form being submitted twice on 2nd submit, 3 times on 3rd submit, etc

本文关键字:提交 等等 三次 第三次 表单 第二次 ajax 两次      更新时间:2023-09-26

由于某种原因,我的表单在第一次"提交"时被提交一次,如果不刷新介于两者之间的页面,则在第二次"提交)时被提交两次。

$(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
    });
});

您需要查看更多的代码,但可能您的提交处理程序在每次发布时都会额外注册一次。

尝试在之前在线上添加控制台日志或警报

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

以确保提交事件没有被多次注册。如果您正在生成javascript服务器端,那么可能就是这种情况。否则,我们将需要更多信息。