简体   繁体   English

当焦点不在输入中时,允许在ENTER上提交FORM

[英]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.

相关问题 如果用户专注于登录输入,如何在输入密钥时提交表单? - How do I get the form to submit on enter key only when user has focus on login input? 当阻止 Enter 键提交 AJAX 表单时,专注于下一个输入不起作用 - When preventing Enter key to submit AJAX form focus on next input isn't working 允许Shift + Enter提交表单 - Allow Shift + Enter to Submit Form 如果焦点不在提交按钮上,则表单不应在回车时提交 - form should not submit on enter if focus is not on submit button jQuery对话框Enter提交(不关注表单) - jQuery dialog Enter to submit (without form focus) 当用户点击输入只有一个输入的表单时,请避免“提交” - avoid “submit” when user hits enter in form with only one input 如何通过按Enter提交输入表单,但在按shift和Enter时不提交输入表单 - How to submit an input form by pressing Enter BUT not submit it when shift and enter are pressed 取消表单提交但允许输入关键事件 - Cancel form submit but allow enter key event 当关注特定输入时,如何防止表单提交? - How to prevent form submit, when focus is on specific input? 用户在输入=文本中按下Enter时的jQuery函数commit(),也提交位于同一表格中的输入=提交 - JQuery function submit() for when the user presses Enter in an input=text submitting also an input=submit located in the same form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM