输入按键时获取输入文本的值 [英] get the value of input text when enter key pressed
本文介绍了输入按键时获取输入文本的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试过这个:
I am trying this:
<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...
推荐答案
试试这个:
Try this:
<input type="text" placeholder="some text" class="search" onkeydown="search(this)"/>
<input type="text" placeholder="some text" class="search" onkeydown="search(this)"/>
JS代码
JS Code
function search(ele) {
if(event.key === 'Enter') {
alert(ele.value);
}
}
DEMO Link
这篇关于输入按键时获取输入文本的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文