最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - inline javascript alert on enter keypress using inline coding - Stack Overflow

programmeradmin5浏览0评论

i am trying to alert 1 on enter keypress can anyone help me to solve this? i am doing this inline because to work in rows and on every time enter key press send a row num with that request

      <input type="text" id="tt" name="tt" onkeydown="javascript: if (keyCode == 13) alert(1)"/>

i am trying to alert 1 on enter keypress can anyone help me to solve this? i am doing this inline because to work in rows and on every time enter key press send a row num with that request

      <input type="text" id="tt" name="tt" onkeydown="javascript: if (keyCode == 13) alert(1)"/>
Share Improve this question asked Mar 7, 2013 at 11:56 Umer FarooqUmer Farooq 842 silver badges9 bronze badges 2
  • You need to use the Event interface (first parameter or window.event). – Marcel Korpel Commented Mar 7, 2013 at 11:57
  • This could be acplished without it being inline. – Jeff Shaver Commented Mar 7, 2013 at 12:07
Add a ment  | 

3 Answers 3

Reset to default 9

If you want to use inline js, just put event.keycode instead of keycode you can use:

<input type="text" id="tt" name="tt" onkeydown="javascript: if(event.keyCode == 13) alert(1);"/>

why inline?? when you can call event in jquery

try this

$('#tt').keydown(function(e){
    if (e.keyCode == 13) 
    {
      alert(1);
    }
}

and if incase you need inline... you need to call event.keyCode to get the keycode of key pressed

 <input..name="tt" onkeydown="yourFuncName(event)"....>

 function yourFuncName(e){
     if (e.keyCode == 13) 
     {
        alert(1);
    }
 }
<input type="text" id="tt" name="tt" onkeydown="myFunction(event)"/>

<script type="text/javascript">
    function myFunction(e) {
        if (e.which === 13 || e.charCode === 13 || e.keyCode === 13)
            alert(1);
    }
</script>

Please not that not all browsers support keyCode. Some use which and others use charCode.

发布评论

评论列表(0)

  1. 暂无评论