<i id='atmYv'><tr id='atmYv'><dt id='atmYv'><q id='atmYv'><span id='atmYv'><b id='atmYv'><form id='atmYv'><ins id='atmYv'></ins><ul id='atmYv'></ul><sub id='atmYv'></sub></form><legend id='atmYv'></legend><bdo id='atmYv'><pre id='atmYv'><center id='atmYv'></center></pre></bdo></b><th id='atmYv'></th></span></q></dt></tr></i><div id='atmYv'><tfoot id='atmYv'></tfoot><dl id='atmYv'><fieldset id='atmYv'></fieldset></dl></div>
  1. <small id='atmYv'></small><noframes id='atmYv'>

    <tfoot id='atmYv'></tfoot>

  2. <legend id='atmYv'><style id='atmYv'><dir id='atmYv'><q id='atmYv'></q></dir></style></legend>
      <bdo id='atmYv'></bdo><ul id='atmYv'></ul>

    1. 如何捕获 Enter 按键?

      时间:2023-08-07

        <tfoot id='heUPW'></tfoot>

        <small id='heUPW'></small><noframes id='heUPW'>

          <bdo id='heUPW'></bdo><ul id='heUPW'></ul>
          <i id='heUPW'><tr id='heUPW'><dt id='heUPW'><q id='heUPW'><span id='heUPW'><b id='heUPW'><form id='heUPW'><ins id='heUPW'></ins><ul id='heUPW'></ul><sub id='heUPW'></sub></form><legend id='heUPW'></legend><bdo id='heUPW'><pre id='heUPW'><center id='heUPW'></center></pre></bdo></b><th id='heUPW'></th></span></q></dt></tr></i><div id='heUPW'><tfoot id='heUPW'></tfoot><dl id='heUPW'><fieldset id='heUPW'></fieldset></dl></div>
            <legend id='heUPW'><style id='heUPW'><dir id='heUPW'><q id='heUPW'></q></dir></style></legend>

              <tbody id='heUPW'></tbody>
              • 本文介绍了如何捕获 Enter 按键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                在我的 HTML 页面中,我有一个文本框供用户输入关键字进行搜索.当他们点击搜索按钮时,JavaScript 函数将生成一个 URL 并在新窗口中运行.

                In my HTML page, I had a textbox for user to input keyword for searching. When they click the search button, the JavaScript function will generate a URL and run in new window.

                当用户用鼠标点击搜索按钮时,JavaScript函数正常工作,但当用户按下ENTER键时没有响应.

                The JavaScript function work properly when the user clicks the search button by mouse, but there is no response when the user presses the ENTER key.

                function searching(){
                    var keywordsStr = document.getElementById('keywords').value;
                    var cmd ="http://XXX/advancedsearch_result.asp?language=ENG&+"+ encodeURI(keywordsStr) + "&x=11&y=4";
                    window.location = cmd;
                }
                

                <form name="form1" method="get">
                    <input name="keywords" type="text" id="keywords" size="50" >
                    <input type="submit" name="btn_search" id="btn_search" value="Search" 
                        onClick="javascript:searching(); return false;" onKeyPress="javascript:searching(); return false;">
                    <input type="reset" name="btn_reset" id="btn_reset" value="Reset">
                </form>
                

                推荐答案

                表单方法

                正如 scoota269 所说,您应该改用 onSubmit,因为在文本框上按 Enter 很可能会触发表单提交(如果在表单内)

                Form approach

                As scoota269 says, you should use onSubmit instead, cause pressing enter on a textbox will most likey trigger a form submit (if inside a form)

                <form action="#" onsubmit="handle">
                    <input type="text" name="txt" />
                </form>
                
                <script>
                    function handle(e){
                        e.preventDefault(); // Otherwise the form will be submitted
                
                        alert("FORM WAS SUBMITTED");
                    }
                </script>
                

                文本框方法

                如果您想在输入字段上设置一个事件,那么您需要确保您的 handle() 将返回 false,否则表单将被提交.

                Textbox approach

                If you want to have an event on the input-field then you need to make sure your handle() will return false, otherwise the form will get submitted.

                <form action="#">
                    <input type="text" name="txt" onkeypress="handle(event)" />
                </form>
                
                <script>
                    function handle(e){
                        if(e.keyCode === 13){
                            e.preventDefault(); // Ensure it is only this code that runs
                
                            alert("Enter was pressed was presses");
                        }
                    }
                </script>
                

                这篇关于如何捕获 Enter 按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:将文本字段限制为仅数字的最佳方法? 下一篇:谷歌浏览器 &gt;文本框 &gt;活动时黄色边框

                相关文章

                最新文章

                <small id='dWkGB'></small><noframes id='dWkGB'>

                • <bdo id='dWkGB'></bdo><ul id='dWkGB'></ul>

                <tfoot id='dWkGB'></tfoot>
                1. <legend id='dWkGB'><style id='dWkGB'><dir id='dWkGB'><q id='dWkGB'></q></dir></style></legend>
                    <i id='dWkGB'><tr id='dWkGB'><dt id='dWkGB'><q id='dWkGB'><span id='dWkGB'><b id='dWkGB'><form id='dWkGB'><ins id='dWkGB'></ins><ul id='dWkGB'></ul><sub id='dWkGB'></sub></form><legend id='dWkGB'></legend><bdo id='dWkGB'><pre id='dWkGB'><center id='dWkGB'></center></pre></bdo></b><th id='dWkGB'></th></span></q></dt></tr></i><div id='dWkGB'><tfoot id='dWkGB'></tfoot><dl id='dWkGB'><fieldset id='dWkGB'></fieldset></dl></div>