javascript 如何从javascript设置表单的默认按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7938427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:48:18  来源:igfitidea点击:

How to set the default button of a form from javascript?

javascriptjquery

提问by user1019662

How to set the default button of a form using javascript or jQuery??

如何使用 javascript 或 jQuery 设置表单的默认按钮?

回答by Royi Namir

$(document).ready(function ()
{
    $(".tbPassword").keydown(function (e)           /* or keyup  */
    {
        if (e.keyCode == 13) // 27=esc
        {
            $("form").submit();
        }
    });
});

回答by Ahmad Hindash

I'v got the answer from the web URL JQuery: Setting Default Submit Button For ‘Enter Key' Using JQuery

我从 Web URL JQuery得到了答案:使用 JQuery 设置“输入键”的默认提交按钮

which he writes the following script:

他编写了以下脚本:

<div>
 <input type="submit" name="CancelButton" value="Cancel" />
 <input type="submit" name="DeleteButton" value="Delete" />
 <input type="submit" name="UpdateButton" value="Update" />
</div>

Now if you have your focus on one of the HTML form fields – First Name or Last Name and hit ‘Enter Key', form will be submitted to one of the button actions but you are not sure which ones!

现在,如果您将注意力集中在 HTML 表单字段之一 - 名字或姓氏并点击“输入键”,表单将提交给按钮操作之一,但您不确定是哪个!

Now let us say you wanted to default the form submission always to “Update” button, you need to do the following –

现在让我们假设您希望将表单提交始终默认为“更新”按钮,您需要执行以下操作 –

1- Assign an ID to Update button

1- 为更新按钮分配一个 ID

<input type=”submit” name=”UpdateButton” value=”Update” id=”defaultActionButton”>

2 -Capture onKeyDown event for textboxes and submit the form to “Update” button / action. [this is where I use jQuery]

2 - 捕获文本框的 onKeyDown 事件并将表单提交到“更新”按钮/操作。[这是我使用 jQuery 的地方]

// all jQuery events are executed within the document ready function 
$(document).ready(function() { $("input").bind("keydown", function(event) {
  // track enter key
  var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
  if (keycode == 13) { // keycode for enter key
     // force the 'Enter Key' to implicitly click the Update button
     document.getElementById('defaultActionButton').click();
     return false;
  } else  {
     return true;
  }}); // end of function  }); // end of document ready

回答by gchq

I know this is an old one, but having spent a good amount of time trying to figure it out (we have a number of different textboxes and buttons) this seemed to be the most straightforward method based on them hitting 'Enter' whilst still in the last input

我知道这是一个旧的,但是花了很多时间试图弄清楚它(我们有许多不同的文本框和按钮)这似乎是最直接的方法,基于它们在仍然处于最后输入

 $(document).keypress(function (e) {
        if (e.keyCode === 13) {
            if ($("#inputLoginPassword").is(":focus")) {
                $("#LoginButton").click();
            }
            if ($("#inputIntegratedPassword").is(":focus")) {
                $("#integratedLoginButton").click();
            }
            if ($("#inputPinNumber").is(":focus")) {
                $("#PinLoginButton").click();
            }

            return false;
        }
    });

回答by Cystack

$("form").append('<input type="submit" value="OK" />');allows you to add a default submit button to all your forms, using jQuery. You can tweak this button a lot before appending it

$("form").append('<input type="submit" value="OK" />');允许您使用 jQuery 向所有表单添加默认提交按钮。您可以在添加此按钮之前对其进行大量调整

回答by M S

make a div (with id) in the place where do you want to place the button.

在要放置按钮的位置创建一个 div(带有 id)。

<div id="placeHere"></div>

and from jQuery do this.

并从 jQuery 执行此操作。

$("#placeHere").html('<input type="submit" value="Submit" />')

Hope his helps

希望他的帮助