开发者

Not able to track enter key on input type = text

开发者 https://www.devze.com 2023-01-09 13:29 出处:网络
Can anybody tell me why is this not working? 开发者_StackOverflow社区 <input name =\"txtChat\" id =\"txtChat\" onkeydown =\"isEnterPressed();\"type=\"text\" style=\"width:720px;\"/>

Can anybody tell me why is this not working?

开发者_StackOverflow社区
  <input name ="txtChat" id ="txtChat" onkeydown ="isEnterPressed();"  type="text" style="width:720px;"/>

This is my javascript function :-

 function isEnterPressed(event){
                alert(event);

            }

On alert i always gets undefined :(

Thanks in advance :)


There is no built-in event type that is fired when the enter key is pressed, you need to check that in the Event object that is passed to the handler.

HTML:

<input name ="txtChat" id ="txtChat" onkeyup="onKeyPressed(event)"  type="text" style="width:720px;"/>

JavaScript:

function onKeyPressed(ev) {
   var e = ev || event;
   if(e.keyCode == 13) {
      //Enter was pressed
      return false; //prevents form from being submitted.
   }
}


A very simple inline check is:

<input name ="txtChat" id ="txtChat" onkeydown ="alert(event.keyCode == 13);"  type="text" style="width:720px;"/>

which outputs true if enter is pressed.

0

精彩评论

暂无评论...
验证码 换一张
取 消