admin管理员组文章数量:1316356
Ok, so putting preventDefault on my submit button works to stop form submission. The consequence of this is that it won't tell the user of required fields and just performs the click event on the button.
Adding preventDefault to the submit handler shows the user the required fields, but still fires the click event tied to the button.
I need both.
<form action="" method="post">
<input type="text" class="class" id="id" name="name" required="required" />
</form>
Now the js:
$("button").click(function(event){
event.preventDefault();
//performs function
});
This will fire the click event, regardless of a required form input.
$("form").submit(function(event){
event.preventDefault();
});
$("button").click(function(){
//performs function
});
This will show a required message to the user, but still perform the submit button function. How do I do both? I've looked at some other SO questions I could find on this but it seems most could be answered by adding preventDefault to form.submit.
Ok, so putting preventDefault on my submit button works to stop form submission. The consequence of this is that it won't tell the user of required fields and just performs the click event on the button.
Adding preventDefault to the submit handler shows the user the required fields, but still fires the click event tied to the button.
I need both.
<form action="" method="post">
<input type="text" class="class" id="id" name="name" required="required" />
</form>
Now the js:
$("button").click(function(event){
event.preventDefault();
//performs function
});
This will fire the click event, regardless of a required form input.
$("form").submit(function(event){
event.preventDefault();
});
$("button").click(function(){
//performs function
});
This will show a required message to the user, but still perform the submit button function. How do I do both? I've looked at some other SO questions I could find on this but it seems most could be answered by adding preventDefault to form.submit.
Share Improve this question edited Sep 11, 2013 at 17:16 Caio Tarifa 6,04312 gold badges49 silver badges75 bronze badges asked Jun 1, 2013 at 6:17 o_Oo_O 5,74714 gold badges55 silver badges94 bronze badges 1-
Try this
$("form").submit(function(event){ return false; });
– Sushanth -- Commented Jun 1, 2013 at 6:21
2 Answers
Reset to default 7I was actually trying to do two things when I only needed to do one.
I didn't need the button.click function at all since by default it submits the form. I just needed to put the button.click function inside form.submit instead.
$("form").submit(function(event){
//perform button function
return false;
});
Manually fire the submit event.
$("button").click(function(event) {
event.preventDefault();
$('form').submit();
});
本文标签: javascriptCombine jQuery preventDefault and required form inputsStack Overflow
版权声明:本文标题:javascript - Combine jQuery preventDefault and required form inputs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741961801a2407321.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论