How to trigger HTML button when you press Enter in textbox?

后端 未结 12 1295
悲哀的现实
悲哀的现实 2020-12-04 08:13

So the code that I have so far is:

&
12条回答
  •  广开言路
    2020-12-04 08:48

    I found w3schools.com howto, their try me page is at the following.

    https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_trigger_button_enter

    This worked in my regular browser but did not work in my php app which uses the built in php browser.

    After toying a bit I came up with the following pure JavaScript alternative that works for my situation and should work in every other situation:

        function checkForEnterKey(){
            if (event.keyCode === 13) {
                event.preventDefault();
                document.getElementById("myBtn").click();
            }
        }
    
        function buttonClickEvent()
        {
            alert('The button has been clicked!');
        }
    

    HTML Press the enter key inside the textbox to activate the button.

        

提交回复
热议问题