JQuery Mobile "return false" when submitting a form

I have a form on my jQuery Mobile website that I would like to validate and stop submitting if validation fails.

However, "return false" does not stop form submission because jQuery Mobile submits the form via Ajax. How can I stop submitting a form?

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

The code above triggers a warning but does not stop the form from submitting. If I use data-ajax = "false" in the form tag, it works correctly, but I would prefer to use the Ajax view if possible.

+6
source share
3 answers

Use data-ajax="false" and bind your own ajax form submission code, which will only execute when validation is validated:

 $("#form").live('submit', function() { if (formValidates) { $.post(...); } return false; }); 
+7
source

I agree with bmaland and have the same problem. Here's what worked well for me:

 $(document).on("pagecreate","#Home",function(){ $("#myFormSearch").submit(function(){ myRealSubmit(); return false; }); }); 
+1
source

Please note: 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.

0
source

Source: https://habr.com/ru/post/887848/


All Articles