最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Click event on button is trigged when submitting a form with enter - Stack Overflow

programmeradmin2浏览0评论

I have a form with two buttons, one input[type=submit] and one plain button which is the cancel button.

I have two event handlers, one bound to the form on submit and one bound to the button on click.

When I submit the form by pressing enter in an input the click event on the button fires (and before the submit event I might add), why is this?

This happens in both gecko and webkit.

Here's a working example: /

If you submit by pressing enter I want the submit event to trigger, not the click event.

I have a form with two buttons, one input[type=submit] and one plain button which is the cancel button.

I have two event handlers, one bound to the form on submit and one bound to the button on click.

When I submit the form by pressing enter in an input the click event on the button fires (and before the submit event I might add), why is this?

This happens in both gecko and webkit.

Here's a working example: http://jsfiddle/q3JPR/

If you submit by pressing enter I want the submit event to trigger, not the click event.

Share Improve this question edited Aug 22, 2019 at 17:37 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 1, 2012 at 13:11 Nicklas A.Nicklas A. 7,0617 gold badges43 silver badges67 bronze badges 1
  • This also happens to me, I've spent roughly an hour searching the whole web and have not found any documentation for this behaviour, let me know if you found something. – asumaran Commented Oct 14, 2013 at 7:32
Add a ment  | 

2 Answers 2

Reset to default 6

If you change your button to be <input type="button"... then your events will behave properly... here is the fiddle:

Working Fiddle

I also found this solution. If you set the type attribute to "button" <button type="button">My Button</button> it won't submit. I think not specifying the type by default sets it to submit. So you don't have to change the element to input.

Source: Add regular button inside form that does not perform a submit

发布评论

评论列表(0)

  1. 暂无评论