简体   繁体   中英

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

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

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

I'v got the answer from the web URL JQuery: Setting Default Submit Button For 'Enter Key' Using 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!

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

<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]

// 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

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;
        }
    });

$("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

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

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

and from jQuery do this.

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

Hope his helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM