javascript - 添加 form.submit 事件以确保在提交表单之前发布?

标签 javascript jquery ajax

我有一个表格。我想使用 jquery 添加事件处理程序。

$("form").submit(function blah() {
   $.post('ping-me-please.php', params, function (response) {
       // only submit the form once this goes through!
   });

}

我怎样才能做到这一点?

最佳答案

像这样:

$("form").submit(function (e) {
    var form = $(this);

    if (!form.data('pinged')) {
        e.preventDefault(); // Cancel the submit
        $.post('ping-me-please.php', params, function (response) {
            // only submit the form once this goes through!

            // First we set the data, then we trigger the submit again.
            form.data('pinged', true).submit();
        });
    }
}

关于javascript - 添加 form.submit 事件以确保在提交表单之前发布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3682414/

相关文章:

javascript - 如何使用 JavaScript 将 HTML 表格转换为 Excel 文件?

javascript - 如何在顶部排序最新通知?

javascript - react 完美的滚动条导致小屏幕尺寸出现问题

jquery:复制并粘贴html

jquery - 读取 select 中的选项值

javascript - 从具有特定 css 样式的类中获取所有元素

javascript - 控制台错误: No 'Access-Control-Allow-Origin' header is present on the requested resource

javascript - Rails ajax 错误 - SyntaxError : Unexpected token C in JSON at position 0

ASP.NET 页面方法与 Web 服务

javascript - 如何使用javascript在asp.net中将参数从弹出窗口传递到父窗口