3

如何在按下回车键时,获取文本框的值?

 3 years ago
source link: http://coding2live.com/detail/79/%E5%A6%82%E4%BD%95%E5%9C%A8%E6%8C%89%E4%B8%8B%E5%9B%9E%E8%BD%A6%E9%94%AE%E6%97%B6%EF%BC%8C%E8%8E%B7%E5%8F%96%E6%96%87%E6%9C%AC%E6%A1%86%E7%9A%84%E5%80%BC%EF%BC%9F
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

如何在按下回车键时,获取文本框的值?

coding2live 2021-02-05 16:09:17 0 127 javascript, html, input, keycode

<input type="text" placeholder="some text" class="search" onkeydown="search()"/>
<input type="text" placeholder="some text" class="search" onkeydown="search()"/>

我想用js监听用户是否按下了回车键:

function search() {
    if(event.keyCode == 13) {
        alert("should get the innerHTML or text value here");
    }
}

现在这段代码运行起来没问题。

但我的问题是如何获取input里面的值,我的想法是给函数传递一个引用 “this”;

或者如果他们有id,那么我可以使用id,但我不知道如何区分哪个input是当前被输入的…


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK