user529649
user529649

Reputation:

Using live() with validation and submitting via ajax()

I have this in JQuery:

$("form").validate({  rules: {
            captcha: {
                required: true,
                remote: {
        url: "gb_include/captcha.php",
        type: "post"
          },
            }
        },
        messages: {
            captcha: "Correct captcha is required."
        }});
   $("form").submit(function(){

        if($(this).valid() == true){ /* submit via ajax */ }

But I have to us .live() because the form is loaded after $(document).ready() how can i do that ?

Upvotes: 2

Views: 665

Answers (2)

Jeff the Bear
Jeff the Bear

Reputation: 5653

You can create a custom event to run after the form is loaded.

$(document).bind('bindForm', function (e) {
    $("#form").validate({  rules: {
        captcha: {
            required: true,
            remote: {
    url: "gb_include/captcha.php",
    type: "post"
      },
        }
    },
    messages: {
        captcha: "Correct captcha is required."
    }});
    $("form").submit(function(){
        if($(this).valid() == true){ /* submit via ajax */ }
    });
});

Add this after the code that loads the form:

$(document).trigger('bindForm');

Upvotes: 2

icyrock.com
icyrock.com

Reputation: 28618

Why don't you execute the above code in the .load callback?

See this SOq:

which has a similar requirement.

Upvotes: 1

Related Questions