admin管理员组文章数量:1317909
I can't get my on('submit') event handler to prevent submitting the form using event.preventDefault();
$("section.shopping-cart").on('submit','form.adjustform input[type="submit"]', function(event) {
// code
event.preventDefault();
})
i had had it working using on('click') and using return false. Altho i've read that the above method is the correct way of doing it..
Thanks, Cam
I can't get my on('submit') event handler to prevent submitting the form using event.preventDefault();
$("section.shopping-cart").on('submit','form.adjustform input[type="submit"]', function(event) {
// code
event.preventDefault();
})
i had had it working using on('click') and using return false. Altho i've read that the above method is the correct way of doing it..
Thanks, Cam
Share Improve this question asked Dec 15, 2012 at 14:17 CamCam 9701 gold badge13 silver badges29 bronze badges2 Answers
Reset to default 7You're using the delegating form of on
, with the selector selecting something other than a form element:
// Here ------------------------------------------------v
$("section.shopping-cart").on('submit','form.adjustform input[type="submit"]', ...
The submit
event occurs on form
elements (at least, the one you can usefully cancel does). Specify a form element:
$("section.shopping-cart").on('submit','form.adjustform', ...
Here's an example of it not working because the selector selects non-form elements:
Example | Source
And here's the corrected example working correctly:
Example | Source
(Note: The forms open in new windows, just to avoid taking you away from the code.)
The submit event happens on the form, not on the submit button.
Either bind the event to the form:
$("section.shopping-cart").on('submit','form.adjustform', function(event) {
or bind the click event of the submit button:
$("section.shopping-cart").on('click','form.adjustform input[type="submit"]', function(event) {
(However, some browsers also allows pressing enter to submit the form, in which case it would not trigger the click event.)
本文标签: javascriptpreventDefault() not working on(39submit39)Stack Overflow
版权声明:本文标题:javascript - preventDefault() not working on('submit') - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742008408a2412427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论