2011-08-27 150 views
3

当有人点击'输入'键时需要做什么才能让这个表单提交?提交搜索输入密钥?

<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> 
+1

您需要一个''元素。不知道你在用Javascript做什么。 –

+0

另外:为什么不使用而不是 Jason

回答

2

你可以使用如下形式,具有输入型提交,在这种情况下,如果按任意输入输入 - 如果你有更多的人 - 这将是形式的默认行为提交:

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

,或者因为它表明,你要使用的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码,完全一样,可以在t上找到他的类似问题:How to trigger enter key press of textbox?

希望我回答了你的问题,甚至超时。

相关问题