Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Mobile 'return false' on form submission

I have a form on my jQuery Mobile site that I would like to validate, and stop submission if validation does not pass.

However, 'return false' does not stop the form from being submitted, because jQuery Mobile is submitting the form via Ajax. How can I stop the form submission?

$("#form").live('submit', function(){
    alert('test');
    return false;
});

The above code fires the alert, but does not stop the form from being submitted. If I use data-ajax="false" on the form tag it works correctly, but I would rather use the Ajax submission if possible.

like image 761
Matthew Avatar asked May 11 '11 15:05

Matthew


2 Answers

Use data-ajax="false" and bind your own ajax form submission code that only runs if validation passes:

$("#form").live('submit', function()
{
    if (formValidates)
    {
        $.post(...);
    }

    return false;
});
like image 149
Matt Ball Avatar answered Oct 17 '22 16:10

Matt Ball


I agree with bmaland and had the same problem. Here is what worked well for me:

$(document).on("pagecreate","#Home",function(){
$("#myFormSearch").submit(function(){
        myRealSubmit();
        return false;
    });                      
}); 
like image 1
kishu Avatar answered Oct 17 '22 16:10

kishu