提交搜索输入密钥? [英] Submit Search on Enter Key?

查看:101
本文介绍了提交搜索输入密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



What needs to be done to have this form submitted when someone hits the 'enter' key?

<form id="search" onsubmit="javascript:search(document.getElementById('searchText'))">
  <input type='text' id='searchText' autofocus />
  <input type='button' onclick="search(document.getElementById('searchText'))" value='Search' />
</form>

=searchonsubmit =javascript:search(document.getElementById('searchText'))>
< input type ='text'id ='searchText'autofocus />
< input type ='button'onclick =search(document.getElementById('searchText'))value ='Search'/>
< / form>


推荐答案

您可以像下面一样使用表单,键入submit,在这种情况下,如果您在任何输入中按回车 - 如果您有更多的输入 - 它将是要提交的表单的默认行为:

<form id="search"> <input type='text' id='searchText' /> <input type='submit' value='Search' /> </form>

or, as it shows, you want to use the onsubmit function and handle the "submit" of the form, so you can do this:

或者如您所示,您想使用onsubmit函数并处理提交您可以这样做:

<form id="search" action="#"> <input type="text" id='searchText' name="myinput" onkeypress="handle" /> </form> <script> function handle(e){ if(e.keyCode === 13){ alert("Enter was just pressed."); } return false; } </script>

A code, quite the same, can be found on this similar question: How to trigger enter key press of textbox?

在这个类似的问题上可以找到相同的代码:

Hope I answered your question, even out of time.

这篇关于提交搜索输入密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆