简体   繁体   English

按下Enter键时运行Javascript

[英]Run Javascript when enter pressed

   <class="searchform">
<input type="text" id="textbox" placeholder="Search..." checkbox_test(this.value);">
<input type="submit" id="button" class="classname" onClick="javascript:checkbox_test();">Search</a>

I have a custom search engine with checkbox options. 我有一个带有复选框选项的自定义搜索引擎。 I have a script called "checkbox_test" that will grab the text from the textbox and run the search query plus additional options selected with checkboxes. 我有一个名为“ checkbox_test”的脚本,它将从文本框中获取文本并运行搜索查询以及使用复选框选择的其他选项。

The problem I have is that when I press 'Enter' nothing happens, I have tried many examples from stackoverflow and examples I found on the internet but it does not seem to work for me. 我的问题是,当我按Enter键时,什么都没发生,我尝试了很多stackoverflow的示例以及在Internet上找到的示例,但它似乎对我不起作用。 Any ideas how to solve this problem? 任何想法如何解决这个问题?

Basically I want to run the javascript when the visitor presses enter (with and without focus) 基本上,我想在访问者按下Enter键时运行javascript(有或没有焦点)

HTML: HTML:

<body onkeypress="wasEnter(event)">....

Javascript: Javascript:

function wasEnter(e) {
    if (e.keyCode == 13) {
        // here you can do your stuff
    }
}

You've got a markup error there. 您那里有标记错误。 That's probably why nothing you try is happening. 这就是为什么您尝试什么都没有发生的原因。

Try the following: 请尝试以下操作:

<input type="text" id="textbox" placeholder="Search..." onkeypress="checkbox_test(this.value);">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM