Onclick launch definition

I have a problem with something. I have a form, and I display the form fields inside the system. The form button automatically receives an onclick event like this

window.location='http://www.something.com/test.html?vs=ZWJhMDAzMzktMTNkYi00ODRkLTgxYmQtNjQwYTU5MDIyZWE3OzA6ODc5NjY2NzoxMjg2NzUyNDM3NDI6OTAxNzM0MzA1OjkwMTczNDMwNTsS1' 

It adds the vs variable, which is needed. The problem is that I need a form in order to have a button like input rather than submit. I am using jQuery validation and I have this to force the form to submit

 $("#btn-default2").click(function() { $("#myform").submit(); }); 

Now the problem with this, due to the onclick event, the check fires quickly, and then the onclick event performs a redirect. I just want this redirect if the form is valid. Is there a way to stop the onclick event and transfer it to my function, where I check if the form is valid?

thanks

+5
source share
3 answers

The procedural answer for your problem,

 var btn = $("button.validate"); var x = btn.attr("onclick"); //get the onclick and store it in a variable. btn.attr("onclick", ""); //remove the onclick attribute for that button btn.click(function() { //Now do validations for your button. if ($("input[type='text']").val() == "10") { eval(x); //If the form fields passed the validation //then trigger the redirect using eval } }); 

Demo

0
source

If you want to use the jquery validation plugin, try using a submit handler.

submitHandler is a callback function built into the plugin. Callback to handle the actual view when the form is valid. Returns the form as a single argument. Replaces the default sending.

 $("form").validate({ submitHandler: function(form) { } }); 
0
source
 $("#btn-default2").click(function(){ $(this).off("click"); }); 
-1
source

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


All Articles