How to perform some action before submitting a form via .ajaxForm ()?

I use the ajaxForm () frame to send my data without reloading my page.

$('#ReplayForm').ajaxForm({ success : function(data){ alert("Success"); } }); 

Now, I want to check some condition before submitting the form, and if the condition is false, then stop submitting else continue.

is there any solution for this work or is there any way to buy that i can perform this operation. Thanks in advance.

+6
source share
3 answers

Yes, you can definitely handle this situation. you must call the beforesubmit method for this to see one example

 $('#ReplayForm').ajaxForm({ beforeSubmit : function(arr, $form, options){ if("condition is true") { return true; //it will continue your submission. } else { return false; //ti will stop your submission. } }, success : function(data){ endLoading(); if(data.result=="success") { showSuccessNotification(data.notification); } else { showErrorNotification(data.notification); } } }); 
+8
source

You can use the beforeSubmit option

 $('#ReplayForm').ajaxForm({ beforeSubmit: function (arr, $form, options) { //check your conditions and return false to prevent the form submission if (!valid) { return false; } }, success: function (data) { alert("Success"); } }); 
+4
source

Use the beforeSend option in the JQuery AJAX framework, if the test fails, return false should do this.

 $('#ReplayForm').ajaxForm({ success : function(data){ alert("Success"); }, beforeSend: function() { if(!myFunc()) { return false; } } }); 
+2
source

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


All Articles