使用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
相關文章
相關標籤/搜索