jQuery 绑定到表单提交

问题描述 投票:0回答:2

我正在做以下事情:

    // Post to the server
    $.ajax({
        type: 'POST',
        url: $("form.edit_item").attr("action"),
        data: $("form.edit_item").serialize(),
        success: function(e) {
        }
    });


$(".edit_item")
    .bind('ajax:loading', function() {
        alert('go');
    })
    .bind('ajax:success', function(data, status, xhr) {
        alert('going');
    })
});

表格上:

<form accept-charset="UTF-8" action="/xxxxx/18" class="edit_item" id="edit_item_31" method="post"><div 

虽然表单发布工作正常,但 ajax 绑定不起作用。接下来我可以尝试什么?

jquery
2个回答
2
投票

在您的

$.ajax
选项中,添加
beforeSend
处理程序:

  $.ajax({
    type: 'POST',
    url: $("form.edit_item").attr("action"),
    data: $("form.edit_item").serialize(),
    beforeSend: function() {
        alert('go');
    },
    success: function(e) {
        alert('going');
    }
});

1
投票

您可以绑定到 do 触发的事件,

ajaxSend
ajaxSuccess
,如下所示:

$(".edit_item").bind({
  ajaxSend: function() {
    alert('go');
  },
  ajaxSuccess: function() {
    alert('going');
  }
});

这些火每个请求是为了您希望在批次开始/完成时发出警报,请将

ajaxSend
替换为
ajaxStart
,将
ajaxSuccess
替换为
ajaxStop

© www.soinside.com 2019 - 2024. All rights reserved.