提交搜索输入密钥? [英] Submit Search on Enter Key?
问题描述
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屋!