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

javascript - trying to bind enter click to onkeyup event of a field - Stack Overflow

programmeradmin2浏览0评论

So in normal situation where jquery i allowed and i can bind the field onkeyup i would use:

$('#something').keyup(function(e) {
    var enterKey = 13;
    if (e.which == enterKey){
        somefunction();
     }
 });

however i cannot use this and will have to use something like:

<input id="something" onkeyup="onkeyup_colfield_check(this)" type="text">
 function onkeyup_colfield_check(e){
    var enterKey = 13;
        if (e.which == enterKey){
            somefunction();
        }
}

However this doesn't work like the above.

how can i achieve the same result like the first example but with something like that?

So in normal situation where jquery i allowed and i can bind the field onkeyup i would use:

$('#something').keyup(function(e) {
    var enterKey = 13;
    if (e.which == enterKey){
        somefunction();
     }
 });

however i cannot use this and will have to use something like:

<input id="something" onkeyup="onkeyup_colfield_check(this)" type="text">
 function onkeyup_colfield_check(e){
    var enterKey = 13;
        if (e.which == enterKey){
            somefunction();
        }
}

However this doesn't work like the above.

how can i achieve the same result like the first example but with something like that?

Share Improve this question edited Dec 30, 2012 at 2:37 jeremy 10.1k4 gold badges40 silver badges60 bronze badges asked Dec 30, 2012 at 2:21 Neta MetaNeta Meta 4,04710 gold badges47 silver badges71 bronze badges 2
  • Why can't you use jQuery ? – Popnoodles Commented Dec 30, 2012 at 2:29
  • cannot use in what i am doing right now. jquery is not yet loaded at this stage – Neta Meta Commented Dec 30, 2012 at 2:32
Add a comment  | 

1 Answer 1

Reset to default 15

You need to pass in event as an argument, not this.

<input id="something" onkeyup="onkeyup_colfield_check(event)" type="text">

Also, to be fully compatible with all major browsers, you may want to use the following for detecting te key code of the key pressed.

var charCode = (typeof e.which === "number") ? e.which : e.keyCode;
发布评论

评论列表(0)

  1. 暂无评论