AJAX多表格提交

时间:2016-02-09 17:32:21

标签: javascript php jquery ajax

我不是Javascript大师但是我努力防止这种情况。此脚本随机发送多个帖子。我无法稳定它。通常工作正常,每次点击发送一个帖子。但有时它只是决定它应该像5-6次一样发布...注意使用async: false并没有真正有任何区别。并且它阻止我在提交后禁用按钮,而不是因为“点击次数”。提前谢谢!

$('#submit').click(function () {
    $('#submit').attr("disabled", true);
    var personal_text_data = document.getElementById('personal_text').value;
    var lang_option_data = document.getElementById('language_option').checked;
    $.ajax({
        type: "POST",
        url: 'send.php',
        cache: false,
        // async: false,
        data: ({
            notification_type: notification_type_data,
            customer_id: customer_id_data,
            personal_text: personal_text_data,
            language_option: lang_option_data
        }),
        success: function () {
            delete customer_id_data;
            delete personal_text_data;
            delete notification_type_data;
            delete lang_option_data;
            location.reload();
        }
    });
});

1 个答案:

答案 0 :(得分:1)

使用e.preventDefault();来避免ajax提交和正常表单提交发生。同时更改点击事件并将其设为$('[yourForm]').submit()

$('[selectorToYourForm]').submit(function (e) {
    $('#submit').prop("disabled", true);
    e.preventDefault();
    var personal_text_data = document.getElementById('personal_text').value;
    var lang_option_data = document.getElementById('language_option').checked;
    $.ajax({
        type: "POST",
        url: 'send.php',
        cache: false,
        // async: false,
        data: ({
            notification_type: notification_type_data,
            customer_id: customer_id_data,
            personal_text: personal_text_data,
            language_option: lang_option_data
        }),
        success: function () {
            location.reload();
        }
    });
});