输入触发按钮单击

发布于 2024-10-13 11:43:52 字数 612 浏览 7 评论 0原文

我有一个带有两个按钮的页面。一个是

我找不到任何看起来像设置默认按钮的可靠方法,目前我也不一定想要这样做。在没有更好的情况下,我在表单上的任何位置捕获了按键,如果按下的是 键,我只是将其否定:

$('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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

2024-10-20 11:43:52

使用

<button type="button">Whatever</button>

应该可以解决问题。

原因是表单内的按钮的类型隐式设置为 submit。正如 zzzzBoz 所说,规范表示,在这种情况下会触发第一个带有 type="submit"buttoninput 。如果您专门设置了 type="button",浏览器就会将其从考虑中删除。

Using

<button type="button">Whatever</button>

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 first button or input with type="submit" is what is triggered in this situation. If you specifically set type="button", then it's removed from consideration by the browser.

镜花水月 2024-10-20 11:43:52

阅读 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:

A form element's default button is the first submit button in tree order whose form owner is that form element.

If the user agent supports letting the user submit a form implicitly (for example, on some platforms hitting the "enter" key while a text field is focused implicitly submits the form), then doing so for a form whose default button has a defined activation behavior must cause the user agent to run synthetic click activation steps on that default button.

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.

听风念你 2024-10-20 11:43:52

无论您在何处使用

Where ever you use a <button> element by default it considers that button type="submit" so if you define the button type="button" then it won't consider that <button> as submit button.

哭了丶谁疼 2024-10-20 11:43:52

我认为你不需要 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.

亽野灬性zι浪 2024-10-20 11:43:52

通过在聚焦的 上按“Enter”,您可以在第一个定位元素上触发“click”事件:

请参阅此处的示例。

您的代码阻止在

但为什么需要在文本字段中按 Enter 呢?如果您想通过按“Enter”键提交表单,但

捕获“Enter”并保存标记:

$('input[type="text"]').keypress(function (e) {
    var code = e.keyCode || e.which;
    if (code === 13) {
        e.preventDefault();
        // also submit by pressing Enter:
        $("form").submit();
    }
});

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:

$('input[type="text"]').keypress(function (e) {
    var code = e.keyCode || e.which;
    if (code === 13) {
        e.preventDefault();
        // also submit by pressing Enter:
        $("form").submit();
    }
});
爱的那么颓废 2024-10-20 11:43:52

默认情况下,在表单的文本字段中按 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.

空城仅有旧梦在 2024-10-20 11:43:52

您可以使用 javascript 阻止表单提交,直到适当的时间为止。一个非常粗略的例子:

<form onsubmit='return false;' id='frmNoEnterSubmit' action="index.html">

    <input type='text' name='txtTest' />

    <input type='button' value='Submit' 
        onclick='document.forms["frmNoEnterSubmit"].onsubmit=""; document.forms["frmNoEnterSubmit"].submit();' />

</form>

按 Enter 键仍然会触发表单提交,但 javascript 会阻止它实际提交,直到您实际按下按钮。

You can use javascript to block form submission until the appropriate time. A very crude example:

<form onsubmit='return false;' id='frmNoEnterSubmit' action="index.html">

    <input type='text' name='txtTest' />

    <input type='button' value='Submit' 
        onclick='document.forms["frmNoEnterSubmit"].onsubmit=""; document.forms["frmNoEnterSubmit"].submit();' />

</form>

Pressing enter will still trigger the form to submit, but the javascript will keep it from actually submitting, until you actually press the button.

独自←快乐 2024-10-20 11:43:52

Dom 示例

  <button onclick="anotherFoo()"> Add new row</button>
  <input type="text" name="xxx" onclick="foo(event)">

javascript

function foo(event){
   if(event.which == 13 || event.keyCode == 13) // for crossbrowser
   {
     event.preventDefault(); // this code prevents other buttons triggers use this
     // do stuff
   }
}

function anotherFoo(){
  // stuffs.
}

如果您不使用 PreventDefault(),则会触发其他按钮。

Dom example

  <button onclick="anotherFoo()"> Add new row</button>
  <input type="text" name="xxx" onclick="foo(event)">

javascript

function foo(event){
   if(event.which == 13 || event.keyCode == 13) // for crossbrowser
   {
     event.preventDefault(); // this code prevents other buttons triggers use this
     // do stuff
   }
}

function anotherFoo(){
  // stuffs.
}

if you don't use preventDefault(), other buttons will triggered.

满地尘埃落定 2024-10-20 11:43:52

我会这样做:在按钮的 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.

别忘他 2024-10-20 11:43:52

我的情况是表单元素中有两个 Submit 按钮:UpdateDeleteDelete 按钮删除图像,Update 按钮使用表单中的文本字段更新数据库。

由于 Delete 按钮位于表单中的第一个按钮,因此它是 Enter 键上的默认按钮。不是我想要的。用户希望在更改某些文本字段后能够按 Enter

我在此处找到了设置默认按钮的答案:

<form action="/action_page.php" method="get" id="form1">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
</form>
<button type="submit" form="form1" value="Submit">Submit</button>

在不使用任何脚本的情况下,我定义了每个按钮所属的表单使用

这是迄今为止唯一对我有用的事情。

My situation has two Submit buttons within the form element: Update and Delete. The Delete button deletes an image and the Update 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 on Enter key. Not what I wanted. The user would expect to be able to hit Enter after changing some text fields.

I found my answer to setting the default button here:

<form action="/action_page.php" method="get" id="form1">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
</form>
<button type="submit" form="form1" value="Submit">Submit</button>

Without using any script, I defined the form that each button belongs to using the <button> form="bla" attribute. I set the Delete button to a form that doesn't exist and set the Update button I wanted to trigger on the Enter 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.

深爱不及久伴 2024-10-20 11:43:52

你可以做这样的事情。

将您的事件绑定到一个通用函数中,并通过按键或单击按钮来调用该事件。

例如。

function callME(event){
    alert('Hi');
}



$('button').on("click",callME); 
$('input ').keypress(function(event){
  if (event.which == 13) {
    callME(event);
  }
});

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.

function callME(event){
    alert('Hi');
}



$('button').on("click",callME); 
$('input ').keypress(function(event){
  if (event.which == 13) {
    callME(event);
  }
});
请止步禁区 2024-10-20 11:43:52

我添加了一个“提交”类型的按钮作为表单的第一个元素,并使其不可见(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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文