Reputation: 491
I made a form and set its attr to onsubmit="return false"
Now I need to execute some actions before the actual submit like this:
$('input[name=submit]').click(function(){
$('.switch-input').attr('checked','checked');//getting all values of chbz
//here goes a little more code//
$('form[name=setform]').removeAttr('onsubmit');//finally sending to $_POST
});
At first glance everything works correct, but I wonder will it 100% execute all code before sending or did I do it wrong?
Upvotes: 0
Views: 70
Reputation: 1401
You could consider dismissing the onsubmit
attribute and instead use your .click()
code and just put return false at the end. You could also use .submit()
as suggested by Stiliyan.
Upvotes: 1
Reputation: 1174
The onsubmit="return false"
attribute will prevent the form from being submitted as it normally would be. However, when the user clicks the Submit input, the above snippet will execute once, removing the onsubmit
attribute and making the form able to be submitted normally, but the user will have to click Submit again to submit it.
You can keep the existing snippet and tell jQuery to submit the form explicitly, via the third variation of the .submit()
method:
$('input[name=submit]').click(function(){
$('.switch-input').attr('checked','checked');//getting all values of chbz
//here goes a little more code//
$('form[name=setform]').removeAttr('onsubmit');//make the form submittable
$('form[name=setform]').submit();//finally sending to $_POST
});
Alternatively, you can remove the onsubmit="return false"
attribute, and use the .submit(handler)
variation of the method, which simplifies your code and guarantees the handler will execute just before the form is submitted:
$('form[name=setform]').submit(function(){
$('.switch-input').attr('checked','checked');//getting all values of chbz
//here goes a little more code//
});
Upvotes: 1