1

好吧,所以我正在尝试设置一个 keydown 调度程序,它将根据按下的键执行不同的操作。

它已添加到我的输入字段的 onkeyup

$(function() {
  $("#myInput").keyup(suggestion_dispatcher);
});

然后这是建议调度程序本身

function suggestion_dispatcher() {
    alert($(this).val());
    var code = event.which;

    if(code == 13) {
        // select
    } else if (code == 38) {
        // up
    } else if (code == 40) {
        // down
    } else {
        $.proxy(get_suggestion(), $(this));
    }
}

即使使用 jQuery 代理,在我的 get_suggestion() 函数中,如果我查看this或者$(this)我看到它给了我窗口对象而不是正在输入的输入框。我错过了什么?

4

1 回答 1

4

您可能想试试这个:get_suggestion.apply(this);get_suggestion.call(this);

这是一些有用的参考 - http://odetocode.com/blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx

于 2012-06-21T23:06:19.557 回答