1

I have the following code

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"
              });
          }
      });
});

I'm finding that the handler is only getting called every first, third, fifth, 7th time the submit button is clicked (the response is pretty much just the form's html with errors etc inserted).

However, changing the function to an onclick handler works everytime.

1 Answer 1

2

This should works fine:

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')
                  }
              });
          }
      });
});​
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a reason why my code doesn't work? (I'm more interested in the jquery theory behind what's happening than getting it working :S))
I'm with @Taras. Your answer, if explained, might help people with similar (but not exactly the same) problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.