admin管理员组文章数量:1300186
I'd like to know how I can initiate a javacsript function when pressing the enter key. I'm trying to create a function called handleEnter(event, fn)
.
I want to use the function on an input field eg:
onkeypress="return handleEnter(event, update_field(this));
I'd like to know how I can initiate a javacsript function when pressing the enter key. I'm trying to create a function called handleEnter(event, fn)
.
I want to use the function on an input field eg:
onkeypress="return handleEnter(event, update_field(this));
Share
edited Nov 24, 2011 at 13:56
Adam Wagner
16.1k7 gold badges53 silver badges67 bronze badges
asked Jun 28, 2010 at 17:50
RapidzRapidz
411 silver badge3 bronze badges
0
3 Answers
Reset to default 6For your function called onkeypress, check the event's .keyCode or .which value, and see if it is equal to 13.
function handleEnter(e, func){
if (e.keyCode == 13 || e.which == 13)
//Enter was pressed, handle it here
}
IIRC, IE uses event.which, and Firefox will use e.keyCode to see which key was pressed.
I think I've solved it.
On the input field I've got:
<input onkeypress="return handleEnter(event, update_field, this, 'task');" type="text" />
For my function I've got:
function handleEnter(e, callback, obj, field){
if(e){
e = e
} else {
e = window.event
}
if(e.which){
var keycode = e.which
} else {
var keycode = e.keyCode
}
if(keycode == 13) {
var tstid = $(obj).parent().find('input[type=hidden]').val();
callback.apply(this, [field, $(obj).val(), tstid ]);
}
}
and it seems to be working fine now.
You can try this shorthand
<input type=”text” onKeydown=”Javascript: if (event.keyCode==13) Search();”>
<input type=”button” value=”Search” onClick=”Search();”>
From http://www.techtamasha./call-javascript-function-on-pressing-enter-key/25
本文标签: javascriptHow do i call a js function on pressing enter keyStack Overflow
版权声明:本文标题:javascript - How do i call a js function on pressing enter key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741660281a2390994.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论