使用JavaScript在文本框中的Enter键上触发按钮单击

问题:

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事件 ide

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键便可单击该特定按钮,没有别的。 this

<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
相关文章
相关标签/搜索