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;
});
Th开发者_运维知识库e 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.
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;
});
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;
});
});
Note that if you can use $("#form").submit() instead of live() then your code will work as expected, without having to provide your own ajax form submission code.
精彩评论