触发一个按钮点击使用JavaScript上的Enter键文本框中 [英] Trigger a button click with JavaScript on the Enter key in a text box
问题描述
我有一个文本输入和一个按钮(见下文)。如何使用JavaScript来的触发按钮的点击事件在<大骨节病>输入骨节病>键是在文本框内pssed $ P $?
有已经是一个不同的我提交当前页面上的按钮,所以我不能简单地使按钮提交按钮。而且,我的只有的希望<大骨节病>输入骨节病>键,如果它是一个文本此框中距离pssed $ P $,闲来无事点击这个特定的按钮。
&LT;输入类型=文本ID =txtSearch/&GT;
&LT;输入类型=按钮ID =btnSearch值=搜索的onclick =DoSomething的(); /&GT;
在jQuery的,下面的工作:
$(#id_of_textbox)。KEYUP(函数(事件){
如果(event.key code == 13){
$(#id_of_button),点击();
}
});
还是在普通的JavaScript,下面的工作:
的document.getElementById(id_of_textbox)
阅读进度(KEYUP功能(事件){
。事件preventDefault();
如果(event.key code == 13){
。的document.getElementById(id_of_button),点击();
}
});
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?
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.
<input type="text" id="txtSearch" />
<input type="button" id="btnSearch" value="Search" onclick="doSomething();" />
In jQuery, the following would work:
$("#id_of_textbox").keyup(function(event){
if(event.keyCode == 13){
$("#id_of_button").click();
}
});
Or in plain JavaScript, the following would work:
document.getElementById("id_of_textbox")
.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode == 13) {
document.getElementById("id_of_button").click();
}
});
这篇关于触发一个按钮点击使用JavaScript上的Enter键文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!