admin管理员组文章数量:1319469
Not using jQuery, just plain javascript with DOM. There are 2 buttons on the form, one has no type, one has type="submit"
Here is an example of my question:
<div>
<button class="add">add</button>
</div>
<div>
<button type="submit">submit</button>
</div>
both buttons trigger the submit event. how do I get only the button with type="submit" to trigger the submit event?
Also, how do I persist the form data so that the result can stay in the "pre" tag when the submit is triggered?
Not using jQuery, just plain javascript with DOM. There are 2 buttons on the form, one has no type, one has type="submit"
Here is an example of my question:
https://plnkr.co/edit/wAlpawx2wJUZ9FXWXL9h?p=preview
<div>
<button class="add">add</button>
</div>
<div>
<button type="submit">submit</button>
</div>
both buttons trigger the submit event. how do I get only the button with type="submit" to trigger the submit event?
Also, how do I persist the form data so that the result can stay in the "pre" tag when the submit is triggered?
Share Improve this question asked Jun 30, 2016 at 1:11 Annie CAnnie C 8042 gold badges14 silver badges32 bronze badges 1- 3 Buttons by default inside of a form trigger the submit. For the second button (that you don't want to submit) you need to prevent the default action from occurring, which is to submit. – Sterling Archer Commented Jun 30, 2016 at 1:14
4 Answers
Reset to default 4set the type attribute to "button" or manually cancel the event
<button type='button' class='add'>add</button>
-or-
$('button.add').click(function(e){
e.preventDefault(); // cancel default behavior
// my add logic
});
you only need to change the type of the button that you don't want to submit with "button". I know it feels weird, but the default type of a button tag is "submit".
<button type="button">Add</button>
The default type of a button
inside a form is submit
. You need to set type="button"
if you want to prevent it from triggering the form submit.
See this question for more details.
As per specifications:
The missing value default is the Submit Button state.
If the type attribute is in the Submit Button state, the element is specifically a submit button.
W3C documentation
本文标签: javascript2 buttons on form only 1 with submit type but both triggers submit eventStack Overflow
版权声明:本文标题:javascript - 2 buttons on form only 1 with submit type but both triggers submit event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742059969a2418524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论