按下回车键时获取输入文本的值 [英] Get the value of input text when enter key pressed
本文介绍了按下回车键时获取输入文本的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试:
<input type="text" placeholder="some text" class="search" onkeydown="search()"/>
<input type="text" placeholder="some text" class="search" onkeydown="search()"/>
用一些javascript来检查是否按下了回车键:
with some javascript to check whether the enter key is pressed:
function search() {
if(event.keyCode == 13) {
alert("should get the innerHTML or text value here");
}
}
这目前工作正常,但我的问题是如何获取文本字段中的值,我正在考虑将引用this"传递给函数,或者如果他们有 id,那么我可以使用 ID,但后来我不明白我如何区分输入的是哪一个,让我再次回到同样的问题......
this works fine at the moment, but my question how to get the value inside the text field, I was thinking of passing a reference "this" to the function, or if they had id's then I could use ID's but then I don't see how I could differentiate between which one has been typed, bringing my back to the same problem again...
推荐答案
试试这个:
<input type="text" placeholder="some text" class="search" onkeydown="search(this)"/>
<input type="text" placeholder="some text" class="search" onkeydown="search(this)"/>
JS代码
function search(ele) {
if(event.key === 'Enter') {
alert(ele.value);
}
}
这篇关于按下回车键时获取输入文本的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文