简体   繁体   中英

Submit form only when submit button clicked and prevent “Enter” submitting form

I want to stop a user submitting a form upon clicking enter.

This works for that

$(document).ready(function() {
        $(window).keydown(function(event){
            if(event.keyCode == 13) {
                event.preventDefault();
                return false;
            }
        });
    });

However I have other buttons on the page that when I tab to and click enter to avail of their functionality, this is blocked via this function.

The button sits as so:

<input type='button' tabindex="29" value='Add Additional Drug' id='addButton'>

And I only want to submit the form when enter pressed while my submit button is selected.

<input type="submit" name="submit" value="Submit" tabindex="40" class="submit"/>

How would I do this?

EDIT

I see the answer in the attached Stackoverflow but he allow people to press Enter if they have completed all the fields:

I don't want a user to press Enter unless they have a button selected(ie Can't press Enter, tab to button, can press enter, which will trigger the button to do its functionality and not submit the form.

The form works on a Tabbing basis, so a user will tab over all the fields.

Binding the keydown event to the whole document will affect all inputs and forms on the page, you may have several ones in your page so it will mess up the whole page logic.

You can bind it to a specific form instead:

$("#myForm input").not("#addButton").keydown(function(event) {
    if (event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
});

Demo:

 $("#myForm input").not("#addButton").keydown(function(event) { if (event.keyCode == 13) { event.preventDefault(); return false; } }); 
 form input { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="" id="myForm"> <input type="text" name="input1" /> <input type="text" name="input2" /> <input type="text" name="input3" /> <input type="submit" name="submit" value="Submit" tabindex="3" class="submit" /> </form> 

Note:

  • I used #myForm as test id here to target a specific form in the page, you just need to use your form id.
  • Using jQuery .not() method in .not("#addButton") won't affect the button with id="addButton" .

First, keep in mind that what you are attempting breaks UI accessibility standards.

Bearing this in mind, you'll need to stop using a true "submit" button and use a regular button that impersonates the submit button.

Next, you'll need to manually trigger the click events for all non-submit button buttons via code.

Here's a working example. See the comments for details:

 $(document).ready(function() { $(window).on("keydown", function(event){ // Check to see if ENTER was pressed and the submit button was active or not if(event.keyCode === 13 && event.target === document.getElementById("btnSubmit")) { // It was, so submit the form document.querySelector("form").submit(); } else if(event.keyCode === 13 && event.target !== document.getElementById("btnSubmit") ){ // ENTER was pressed, but not while the submit button was active alert("Enter pressed on something other than submit button."); // Cancel form's submit event event.preventDefault(); // Invoke click event of target so that non-form submit behaviors will work event.target.click(); // Tell JQuery to cancel the event return false; } }); // Non-submit button event handling $("#btnOther").on("click", function(){ alert("Other button clicked!"); }); // Set up your "regular" button to act as a "submit" button when it is clicked $("#btnSubmit").on("click", function(){ // Submit the form document.querySelector("form").submit(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action=# method=post> <input name=test> <input type=button id="btnOther" value="Other Button"> <input type=button id="btnSubmit" value=Submit> </form> 

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