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

后端 未结 30 1975
难免孤独
难免孤独 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.

提交回复
热议问题