输入触发按钮单击
我有一个带有两个按钮的页面。一个是
我找不到任何看起来像设置默认按钮的可靠方法,目前我也不一定想要这样做。在没有更好的情况下,我在表单上的任何位置捕获了按键,如果按下的是
键,我只是将其否定:
$('form').keypress( function( e ) {
var code = e.keyCode || e.which;
if( code === 13 ) {
e.preventDefault();
return false;
}
})
据我所知到目前为止,它似乎有效,但感觉非常笨拙。
有谁知道更复杂的技术来做到这一点?
同样,这个解决方案是否有我不知道的陷阱?
谢谢。
I have a page with two buttons. One is a <button>
element and the other is a <input type="submit">
. The buttons appear on the page in that order. If I'm in a text field anywhere in the form and press <Enter>
, the button element's click
event is triggered. I assume that's because the button element sits first.
I can't find anything that looks like a reliable way of setting the default button, nor do I necessarily want to at this point. In the absence of anything better, I've captured a keypress anywhere on the form and, if it was the <Enter>
key that was pressed, I'm just negating it:
$('form').keypress( function( e ) {
var code = e.keyCode || e.which;
if( code === 13 ) {
e.preventDefault();
return false;
}
})
As far as I can tell so far, it seems to be working, but it feels incredibly ham-fisted.
Does anyone know of a more sophisticated technique for doing this?
Similarly, are there any pitfalls to this solution that I'm just not aware of?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
使用
应该可以解决问题。
原因是表单内的按钮的类型隐式设置为
submit
。正如 zzzzBoz 所说,规范表示,在这种情况下会触发第一个带有type="submit"
的button
或input
。如果您专门设置了type="button"
,浏览器就会将其从考虑中删除。Using
should do the trick.
The reason is because a button inside a form has its type implicitly set to
submit
. As zzzzBoz says, the Spec says that the firstbutton
orinput
withtype="submit"
is what is triggered in this situation. If you specifically settype="button"
, then it's removed from consideration by the browser.阅读 HTML 规范以真正理解预期的行为非常重要:
HTML5 规范明确规定了
隐式提交
中发生的情况:HTML4 规范中并未明确说明这一点,但浏览器已经实现了 HTML5 规范中描述的内容(这就是明确包含它的原因)。
编辑添加:
我能想到的最简单的答案是将提交按钮作为表单中的第一个
[type="submit"]
项,使用 css 在表单底部添加填充,并将提交按钮绝对放置在您想要的底部。It is important to read the HTML specifications to truly understand what behavior is to be expected:
The HTML5 spec explicitly states what happens in
implicit submissions
:This was not made explicit in the HTML4 spec, however browsers have already been implementing what is described in the HTML5 spec (which is why it's included explicitly).
Edit to add:
The simplest answer I can think of is to put your submit button as the first
[type="submit"]
item in the form, add padding to the bottom of the form with css, and absolutely position the submit button at the bottom where you'd like it.无论您在何处使用
Where ever you use a
<button>
element by default it considers that buttontype="submit"
so if you define the buttontype="button"
then it won't consider that<button>
as submit button.我认为你不需要 javascript 或 CSS 来解决这个问题。
根据 html 5 按钮规范,没有 type 属性的按钮被视为与带有 type 属性的按钮相同它的类型设置为“提交”,即作为提交其包含表单的按钮。将按钮的类型设置为“按钮”应该可以防止您看到的行为。
我不确定浏览器对此的支持,但在 html 中指定了相同的行为4.01 按钮规范 所以我希望它非常好。
I don't think you need javascript or CSS to fix this.
According to the html 5 spec for buttons a button with no type attribute is treated the same as a button with its type set to "submit", i.e. as a button for submitting its containing form. Setting the button's type to "button" should prevent the behaviour you're seeing.
I'm not sure about browser support for this, but the same behaviour was specified in the html 4.01 spec for buttons so I expect it's pretty good.
通过在聚焦的
上按“Enter”,您可以在第一个定位元素上触发“click”事件:
请参阅此处的示例。
您的代码阻止在
但为什么需要在文本字段中按
Enter
呢?如果您想通过按“Enter”键提交表单,但捕获“Enter”并保存标记:
By pressing 'Enter' on focused
<input type="text">
you trigger 'click' event on the first positioned element:<button>
or<input type="submit">
. If you press 'Enter' in<textarea>
, you just make a new text line.See the example here.
Your code prevents to make a new text line in
<textarea>
, so you have to catch key press only for<input type="text">
.But why do you need to press
Enter
in text field? If you want to submit form by pressing 'Enter', but the<button>
must stay the first in the layout, just play with the markup: put the<input type="submit">
code before the<button>
and use CSS to save the layout you need.Catching 'Enter' and saving markup:
默认情况下,在表单的文本字段中按 Enter 键将提交表单。如果您不希望它以这种方式工作,您必须捕获回车键并像您所做的那样使用它。这是没有办法解决的。即使表单中没有按钮,它也会以这种方式工作。
Pressing enter in a form's text field will, by default, submit the form. If you don't want it to work that way you have to capture the enter key press and consume it like you've done. There is no way around this. It will work this way even if there is no button present in the form.
您可以使用 javascript 阻止表单提交,直到适当的时间为止。一个非常粗略的例子:
按 Enter 键仍然会触发表单提交,但 javascript 会阻止它实际提交,直到您实际按下按钮。
You can use javascript to block form submission until the appropriate time. A very crude example:
Pressing enter will still trigger the form to submit, but the javascript will keep it from actually submitting, until you actually press the button.
Dom 示例
javascript
如果您不使用 PreventDefault(),则会触发其他按钮。
Dom example
javascript
if you don't use preventDefault(), other buttons will triggered.
我会这样做:在按钮的 onclick 事件的处理程序中(而不是提交)检查事件对象的键码。如果是“输入”我会返回 false。
I would do it like the following: In the handler for the onclick event of the button (not submit) check the event object's keycode. If it is "enter" I would return false.
我的情况是表单元素中有两个
Submit
按钮:Update
和Delete
。Delete
按钮删除图像,Update
按钮使用表单中的文本字段更新数据库。由于
Delete
按钮位于表单中的第一个按钮,因此它是Enter
键上的默认按钮。不是我想要的。用户希望在更改某些文本字段后能够按Enter
。我在此处找到了设置默认按钮的答案:
在不使用任何脚本的情况下,我定义了每个按钮所属的表单使用
这是迄今为止唯一对我有用的事情。
My situation has two
Submit
buttons within the form element:Update
andDelete
. TheDelete
button deletes an image and theUpdate
button updates the database with the text fields in the form.Because the
Delete
button was first in the form, it was the default button onEnter
key. Not what I wanted. The user would expect to be able to hitEnter
after changing some text fields.I found my answer to setting the default button here:
Without using any script, I defined the form that each button belongs to using the
<button> form="bla"
attribute. I set theDelete
button to a form that doesn't exist and set theUpdate
button I wanted to trigger on theEnter
key to the form that the user would be in when entering text.This is the only thing that has worked for me so far.
你可以做这样的事情。
将您的事件绑定到一个通用函数中,并通过按键或单击按钮来调用该事件。
例如。
You can do something like this.
bind your event into a common function and call the event either with keypress or button click.
for example.
我添加了一个“提交”类型的按钮作为表单的第一个元素,并使其不可见(
width:0;height:0;padding:0;margin:0;border-style:none;font-size:0 ;
)。其工作方式类似于网站的刷新,即,按下按钮时,除了再次加载网站之外,我不执行任何操作。对我来说效果很好...I added a button of type "submit" as first element of the form and made it invisible (
width:0;height:0;padding:0;margin:0;border-style:none;font-size:0;
). Works like a refresh of the site, i.e. I don't do anything when the button is pressed except that the site is loaded again. For me works fine...