Trigger a button click with JavaScript on the Enter key in a text box

后端 未结 30 1908
难免孤独
难免孤独 2020-11-21 23:53

I have one text input and one button (see below). How can I use JavaScript to trigger the button\'s click event when the Enter key is pressed ins

相关标签:
30条回答
  • 2020-11-22 00:37
    document.onkeypress = function (e) {
     e = e || window.event;
     var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
     if (charCode == 13) {
    
            // Do something here
            printResult();
        }
    };
    

    Heres my two cents. I am working on an app for Windows 8 and want the button to register a click event when I press the Enter button. I am doing this in JS. I tried a couple of suggestions, but had issues. This works just fine.

    0 讨论(0)
  • 2020-11-22 00:39

    To add a completely plain JavaScript solution that addressed @icedwater's issue with form submission, here's a complete solution with form.

    NOTE: This is for "modern browsers", including IE9+. The IE8 version isn't much more complicated, and can be learned here.


    Fiddle: https://jsfiddle.net/rufwork/gm6h25th/1/

    HTML

    <body>
        <form>
            <input type="text" id="txt" />
            <input type="button" id="go" value="Click Me!" />
            <div id="outige"></div>
        </form>
    </body>
    

    JavaScript

    // The document.addEventListener replicates $(document).ready() for
    // modern browsers (including IE9+), and is slightly more robust than `onload`.
    // More here: https://stackoverflow.com/a/21814964/1028230
    document.addEventListener("DOMContentLoaded", function() {
        var go = document.getElementById("go"),
            txt = document.getElementById("txt"),
            outige = document.getElementById("outige");
    
        // Note that jQuery handles "empty" selections "for free".
        // Since we're plain JavaScripting it, we need to make sure this DOM exists first.
        if (txt && go)    {
            txt.addEventListener("keypress", function (e) {
                if (event.keyCode === 13)   {
                    go.click();
                    e.preventDefault(); // <<< Most important missing piece from icedwater
                }
            });
    
            go.addEventListener("click", function () {
                if (outige) {
                    outige.innerHTML += "Clicked!<br />";
                }
            });
        }
    });
    
    0 讨论(0)
  • 2020-11-22 00:39

    In modern, undeprecated (without keyCode or onkeydown) Javascript:

    <input onkeypress="if(event.key == 'Enter') {console.log('Test')}">
    
    0 讨论(0)
  • 2020-11-22 00:40

    Try it:

    <input type="text" id="txtSearch"/>
    <input type="button" id="btnSearch" Value="Search"/>
    
    <script>             
       window.onload = function() {
         document.getElementById('txtSearch').onkeypress = function searchKeyPress(event) {
            if (event.keyCode == 13) {
                document.getElementById('btnSearch').click();
            }
        };
    
        document.getElementById('btnSearch').onclick =doSomething;
    }
    </script>
    
    0 讨论(0)
  • 2020-11-22 00:41

    Make the button a submit element, so it'll be automatic.

    <input type = "submit"
           id = "btnSearch"
           value = "Search"
           onclick = "return doSomething();"
    />
    

    Note that you'll need a <form> element containing the input fields to make this work (thanks Sergey Ilinsky).

    It's not a good practice to redefine standard behaviour, the Enter key should always call the submit button on a form.

    0 讨论(0)
  • 2020-11-22 00:41

    This in-case you want also diable the enter button from Posting to server and execute the Js script.

    <input type="text" id="txtSearch" onkeydown="if (event.keyCode == 13)
     {document.getElementById('btnSearch').click(); return false;}"/>
    <input type="button" id="btnSearch" value="Search" onclick="doSomething();" />
    
    0 讨论(0)
提交回复
热议问题