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

后端 未结 30 1968
难免孤独
难免孤独 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:44

    Figured this out:

    <input type="text" id="txtSearch" onkeypress="return searchKeyPress(event);" />
    <input type="button" id="btnSearch" Value="Search" onclick="doSomething();" />
    
    <script>
    function searchKeyPress(e)
    {
        // look for window.event in case event isn't passed in
        e = e || window.event;
        if (e.keyCode == 13)
        {
            document.getElementById('btnSearch').click();
            return false;
        }
        return true;
    }
    </script>
    
    0 讨论(0)
  • 2020-11-22 00:44

    For those who may like brevity and modern js approach.

    input.addEventListener('keydown', (e) => {if (e.keyCode == 13) doSomething()});
    

    where input is a variable containing your input element.

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

    Nobody noticed the html attibute "accesskey" which is available since a while.

    This is a no javascript way to keyboard shortcuts stuffs.

    The accesskey attributes shortcuts on MDN

    Intented to be used like this. The html attribute itself is enough, howewer we can change the placeholder or other indicator depending of the browser and os. The script is a untested scratch approach to give an idea. You may want to use a browser library detector like the tiny bowser

    let client = navigator.userAgent.toLowerCase(),
        isLinux = client.indexOf("linux") > -1,
        isWin = client.indexOf("windows") > -1,
        isMac = client.indexOf("apple") > -1,
        isFirefox = client.indexOf("firefox") > -1,
        isWebkit = client.indexOf("webkit") > -1,
        isOpera = client.indexOf("opera") > -1,
        input = document.getElementById('guestInput');
    
    if(isFirefox) {
       input.setAttribute("placeholder", "ALT+SHIFT+Z");
    } else if (isWin) {
       input.setAttribute("placeholder", "ALT+Z");
    } else if (isMac) {
      input.setAttribute("placeholder", "CTRL+ALT+Z");
    } else if (isOpera) {
      input.setAttribute("placeholder", "SHIFT+ESCAPE->Z");
    } else {'Point me to operate...'}
    <input type="text" id="guestInput" accesskey="z" placeholder="Acces shortcut:"></input>

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

    Short working pure JS

    txtSearch.onkeydown= e => (e.key=="Enter") ? btnSearch.click() : 1
    

    txtSearch.onkeydown= e => (e.key=="Enter") ? btnSearch.click() : 1
    
    function doSomething() {
      console.log('                                                                    
    0 讨论(0)
  • 2020-11-22 00:48
    onkeydown="javascript:if (event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('btnSearch').click();}};"
    

    This is just something I have from a somewhat recent project... I found it on the net, and I have no idea if there's a better way or not in plain old JavaScript.

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

    In jQuery, you can use event.which==13. If you have a form, you could use $('#formid').submit() (with the correct event listeners added to the submission of said form).

    $('#textfield').keyup(function(event){
       if(event.which==13){
           $('#submit').click();
       }
    });
    $('#submit').click(function(e){
       if($('#textfield').val().trim().length){
          alert("Submitted!");
       } else {
        alert("Field can not be empty!");
       }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <label for="textfield">
    Enter Text:</label>
    <input id="textfield" type="text">
    <button id="submit">
    Submit
    </button>

    0 讨论(0)
提交回复
热议问题