问题:
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 inside the text box? 当在文本框中按下Enter键时,如何使用JavaScript 触发按钮的click事件 ?
There is already a different submit button on my current page, so I can't simply make the button a submit button. 当前页面上已经有一个不同的“提交”按钮,因此我不能简单地将该按钮设为“提交”按钮。 And, I only want the Enter key to click this specific button if it is pressed from within this one text box, nothing else. 而且,如果只从一个文本框中按下该按钮,我只希望按Enter键即可单击该特定按钮,没有别的。
<input type="text" id="txtSearch" />
<input type="button" id="btnSearch" value="Search" onclick="doSomething();" />
解决方案:
参考一: https://stackoom.com/question/eN2/使用JavaScript在文本框中的Enter键上触发按钮单击参考二: https://oldbug.net/q/eN2/Trigger-a-button-click-with-JavaScript-on-the-Enter-key-in-a-text-box
来源:oschina
链接:https://my.oschina.net/stackoom/blog/3221786