Reputation: 465
I have a form where I validate some fields by means of jQuery and after checking that everything is correct I want to submit the form. So first I prevent the form to be submit when the submit button is clicked, I perform the validations and finally if everything is correct I submit the form. But my form is not submitted until the submit button is clicked twice. How can I solve it?
$('#form1').submit(function(e) {
e.preventDefault();
... // Validation actions
if (form_ok) {
$(this).unbind('submit').submit();
}
});
Thanks
Upvotes: 1
Views: 630
Reputation: 3760
CODE
$(document).ready(function(){
$('#form1').submit(function(e) {
var form_ok=$(":text").val().length>0;
if(!form_ok)
{
e.preventDefault();
}
});
});
HTML
<form id="form1">
<input type="text" />
<input type="submit" value="Submit" />
</form>
Upvotes: 1