Re-enable Submit after Prevention by default

I am trying to disable or re-enable the alert, so my form will be sent to good data.

I tried a few examples. Here is my code and some of the examples I tried.

This code works great for what I want. Just the last one and reset the div, which I can implement after I get this.

function lengthRestriction(elem, min, max) { var uInput = elem.value; if (uInput.length >= min && uInput.length <= max) { return true; } else { var cnt = document.getElementById('field'); cnt.innerHTML = "Please enter between " + min + " and " + max + " characters"; elem.focus(); $('#ShoutTweet').submit(function(e) { e.preventDefault(); //bind('#ShoutTweet').submit(); //$('#ShoutTweet').trigger('submit'); }); } }​ 

I also have jsbin installed http://jsbin.com/ebedab/93

+7
source share
4 answers

I'm not quite sure what you are asking, but if your goal is to destroy your custom submit handler, then use this:

 $("#ShoutTweet").unbind("submit"); 

This assumes you have a normal form (not Ajax).

+5
source

Do not try to configure and cancel the send handler from your check function, do it the other way around: call the check from one send handler and only call .preventDefault() if the check fails:

 $(document).ready(function() { $('#ShoutTweet').submit(function(e) { if (/* do validations here, and if any of them fail... */) { e.preventDefault(); } }); }); 

If all your checks pass, just do not call e.preventDefault() and the send event will occur by default.

Alternatively, you can return false from the submit handler to prevent the default:

  $('#ShoutTweet').submit(function(e) { if (!someValidation()) return false; if (!secondValidation()) return false; if (someTestVariable != "somevalue") return false; // etc. }); 
+7
source

Just call submit on the form

 $('#ShoutTweet').submit(); 
+1
source

This works event.preventDefault(); and includes event.preventDefault(); forms after event.preventDefault();

 $('#your-login-form-id').on('submit', onSubmitLoader); function onSubmitLoader(e) { e.preventDefault(); var self = $(this); setTimeout(function () { self.unbind('submit').submit(); // like if wants to enable form after 1s }, 1000) } 
0
source

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


All Articles