2014-02-18 41 views
1

我試圖提醒用戶每當他/她按下輸入。我決定爲此使用JavaScript。我有以下列出的HTML位:javascript事件onkeypress事件將不會運行

<div id="all"> 
      <h1>Welcome to the awesome chat system.</h1> 
      <input type="text" id="name"><br/> 
      <textarea rows='30' id="chatArea" disabled='true'> 
      </textarea> 
      <textarea id="writtenThing" onkeypress="keyfunction(e)"></textarea> 
      <button id="send">Send</button> 
    </div> 

這裏是JavaScript:

 <script> 
      function keyfunction(e) 
      { 

       if(e.keyCode == 13) 
       { 
        alert("things to do..."); 
       } 

      } 

     </script> 

不過,我現在面臨的問題是,即使我按文本區域內進入我的瀏覽器不警告我。我確定我正在使用支持此功能的瀏覽器。

回答

2

傳遞事件的功能:

<textarea id="writtenThing" onkeyup="keyfunction(event)"></textarea> 

添加一些跨瀏覽器支持:

 function keyfunction(e) 
     { 
      if(e.keyCode == 13 || e.which == 13) 
      { 
       alert("things to do..."); 
      } 
     } 

JS小提琴:http://jsfiddle.net/3MF3h/

0

可以用在與的onkeyup取代onkeypress事件/ onkeydown事件

此外,更正代碼:

onkeypress事件= 「keyfunction(事件)」

+0

我chaged它但它並不會提醒 – Bula

+0

你逝去的「e」的功能...相反,通過「事件」 –

+0

onkeypress事件=「keyfunction(事件)」 –

1

使用以下javscript。

document.getElementById("writtenThing").onkeypress = function (e) 
{ 
    if (e.keyCode == 13) 
    { 
    alert("things to do..."); 
    } 
    } 
+0

文檔不規範使用的文件和這個作品http://fiddle.jshell.net/tYhGu/ – Cracker0dks

+0

@ Cracker0dks謝謝現在改變了 –

相關問題