2013-11-14 40 views
0

提交按鈕,通過輸入我有這一塊的HTML代碼:的JavaScript的onsubmit無需通過鍵盤

<form id="cambia_stato" method="get" action=""> 
<input type="text" name="stato" id="frase_stato" value="" 
onclick="this.value='';" onblur="setMessaggio()" maxlength="255" 
/> 
</form> 

,我要檢查「stato」場對提交的值。 我嘗試了兩種方式: 使用的onsubmit不按Enter鍵觸發:

<!-- nothing happens with the following --> 
<form id="cambia_stato" method="get" action="" onsubmit="myFunc()"> 

所以,我想每一個鍵被按下時檢查輸入,改變輸入屬性添加onkeypressed事件:

<!-- nothing happens with the following --> 
<input type="text" name="stato" id="frase_stato" value="" 
onclick="this.value='';" onblur="setMessaggio()" maxlength="255" 
onkeypressed="myFunc()" /> 

就目前來說,函數myFunc的()被定義爲如下:

function myFunc() 
{ 
    alert('test'); 
} 

我是否在某處出錯或需要提交按鈕?

+0

不是真的,雖然感謝 – sowdust

+0

是的。看看其他一些答案和http://stackoverflow.com/search?q=submit+enter – mplungjan

+0

你是對的,對此抱歉。我在這裏很新,我將學習如何更好地搜索 – sowdust

回答

0

添加到您的JavaScript。這將捕獲輸入密鑰。

document.onkeydown = function() { 
     if (event.keyCode === 13) { 
      myFunc(); 
     } 
    }; 
+0

非常感謝! – sowdust