How can the submit action on a form(<form></form>) be automatically fired when all input elements within this form are completed/filled?

By : Steven
Source: Stackoverflow.com
Question!

I want the submit action on a form() to be automatically fired when all input elements within this form are completed/filled. How to do it using ajax?

By : Steven


Answers
Something like the following:

$("#myForm :input").blur(function() {
    if($('#myForm :input[value]').length == $('#myForm :input').length) {
        $(this).closest("form").submit(); 
    }
    return false;
});

Note the following:

  • The :input selector matches all input, textarea, select and button elements.
  • The closest selector returns the closest parent element.
By : karim79


Update: In addition to Karim79.

$("#myForm :input").blur(function() {
    var hasEmpty = false;
    if($('#myForm :input[value]').length == $('#myForm :input').length) {
        $(this).closest("form").submit();

    var name = "", email = ""; //Get the value for name and email from the form
    var dataString = 'name='+ name + '&email=' + email;
           $.ajax({
              type: "POST",
              url: "page.aspx",
              data: dataString,
              success: function() {
                $('#message').html("<h2>Form Submitted!</h2>");//Display message
              }
             }); 
    }
    return false;
});

Try this:

$("form")[0].submit();

You will need to call this function after a fields has been validate.

And check this site.

By : Tebo


This video can help you solving your question :)
By: admin