[英]Allow FORM submit on ENTER when focus is not in an input
I have a form where I have a focusable div
using tabindex="0"
. 我有一个使用
tabindex="0"
的可聚焦div
表单。 Everything works fine, except for the possibility to submit the form
on enter
key. 一切正常,除了可以通过
enter
键提交form
。 It does work when the input
is focused, but doesn't when the div
has the focus. 当
input
焦点时它起作用,但当div
获得焦点时则不起作用。
I know I could use some javascript to capture the enter
key and submit the closest form
, but are there any other ways to overcome this? 我知道我可以使用一些JavaScript来捕获
enter
键并提交最接近的form
,但是还有其他方法可以克服这一问题吗?
Here is a little fiddle to demonstrate the problem. 这里有一些小提琴来演示这个问题。 https://jsfiddle.net/ajqfm3Lb/1/
https://jsfiddle.net/ajqfm3Lb/1/
Well, instead of submitting the "closest form", you could actually check the Enter keypress for any element within that form. 好吧,除了提交“最接近的表单”之外,您实际上可以检查Enter键是否存在该表单中的任何元素。 I realize that this is somewhat similar to the solution you weren't looking for, but I'm not sure if you're aware that you can tie that keypress event to specifically the form.
我意识到这与您不需要的解决方案有些相似,但是我不确定您是否知道可以将该按键事件绑定到特定的表单。
Check it out: 看看这个:
https://jsfiddle.net/ajqfm3Lb/9/ https://jsfiddle.net/ajqfm3Lb/9/
$("form").on("keypress", "*:not(textarea)", function(e) {
if (e.which == 13) {
alert("submitted");
return false;
}
});
This event only fires when a key is pressed while focus is on the form , because the keypress
event is only tied to the selector $("form")
. 仅当焦点位于表单上时按下此键时,才会触发此事件,因为
keypress
事件仅与选择器$("form")
绑定。 The e.which == 13
just checks enter, specifically. e.which == 13
仅检查输入。
Of course, you could modify $("form")
to be any selector (eg, the form's ID, class, etc). 当然,您可以将
$("form")
修改$("form")
任何选择器(例如,表单的ID,类等)。
EDIT: As you pointed out, simply listening for the "enter" press on a form would cause issues with elements that require its use, like textarea
. 编辑:正如您所指出的,仅侦听表单上的“ enter”新闻会导致需要使用它的元素(例如
textarea
。 I've used Event Delegation to trigger the event on all children of form except textarea
. 我使用事件委托来触发除
textarea
以外的所有形式的子事件。
Let me know how this works. 让我知道这是如何工作的。
It's not valid HTML, strictly speaking, but it works... You can put your div
inside a <button type="submit">
and remove the button's default styling. 从严格意义上讲,它不是有效的HTML,但是它可以工作...您可以将
div
放在<button type="submit">
并删除按钮的默认样式。 Like the other answer that was given, the div
must be inside the form
for this work without additional JavScript. 像给出的其他答案一样,该
div
必须在form
内,而无需附加JavScript。 You'd also no longer need the tabindex on the div. 您也将不再需要div上的tabindex。
<form>
<input/>
<button type="submit">
<div>
</div>
</button>
</form>
and 和
button[type="submit"] {
border: none;
background: none;
padding: 0;
margin: 0;
display: block;
}
https://jsfiddle.net/g2j2c0ra/2/ https://jsfiddle.net/g2j2c0ra/2/
Just give your form a name (eg id="test
") and then use the following code to submit only the form with the given id: 只需给您的表单命名(例如
id="test
”),然后使用以下代码仅提交具有给定id的表单:
$(function(){
$("#test").keypress(function (e) {
if (e.keyCode == 13) {
alert('submitted');
return false
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.