尝试绑定输入单击到字段的onkeyup事件

Net*_*eta 4 html javascript

所以在正常情况下我允许jquery,我可以绑定字段onkeyup我会使用:

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

但是我不能使用它,必须使用类似的东西:

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

然而,这不像上面那样工作.

我怎么能像第一个例子那样获得相同的结果,但是有类似的东西?

jer*_*emy 12

你需要event作为参数传入,而不是this.

<input id="something" onkeyup="onkeyup_colfield_check(event)" type="text">
Run Code Online (Sandbox Code Playgroud)

此外,为了与所有主流浏览器完全兼容,您可能需要使用以下内容来检测按下的键的te键代码.

var charCode = (typeof e.which === "number") ? e.which : e.keyCode;
Run Code Online (Sandbox Code Playgroud)