1

我有以下代码

jQuery(function() {
      jQuery("#newForm").validate({
          rules: { },
          submitHandler: function(form) {
              alert("Submitting")
              jQuery("#submitButton").attr('disabled', true)
              jQuery("#sendWrapper").append('<span><img src="{{ STATIC_URL }}img/loading.gif"></span>')
              jQuery(form).ajaxSubmit({
                  success: afterFormSubmit,
                  target: "#ajaxwrapper"
              });
          }
      });
});

我发现处理程序仅在第一次、第三次、第五次、第 7 次单击提​​交按钮时被调用(响应几乎只是表单的 html 插入了错误等)。

但是,将函数更改为 onclick 处理程序每​​次都有效。

4

1 回答 1

2

这应该可以正常工作:

jQuery(function() {
      jQuery("#newForm").validate({
          rules: {},
          submitHandler: function(form) {
              alert("Submitting")
              jQuery("#submitButton").attr('disabled', true)
              jQuery("#sendWrapper").append('<span><img src="{{ STATIC_URL }}img/loading.gif"></span>')
              $.ajax({
                  cache: false,
                  type: 'POST',
                  data: ("#newForm").serialize(),
                  url : '/ your-url-address-to-post-form /'
                  success: function (html) {
                      alert('done');
                  },
                  error: function(data, textStatus, jqXHR) {
                      alert('error')
                  }
              });
          }
      });
});​
于 2012-03-03T11:40:32.303 回答