开发者

jquery.hotkeys.js replaces input field with .bind

I'm using jquery.hotkeys.js and mapping my keybinding in the following fashion:

$(document).bind('keydown', 'i', function() {
      $("input#foo").focus()
});

Yet, after calling focus (in attempts to try and move the cursor to the end of the input field when hitting key i being focused on the document) it simply replaces the content of the input field with i. It's possible to prevent the total replacement, however, the i still gets appended even after doing so. Example:

$(document).bind('keydown', 'i', function() {
      var val = $("input#foo").val()
      $("input#foo").focus()
      $("input#foo").val(val.substring(0,val.length-1)
});

In this case, an input of television would turn into televisioi. Anyone of know any workarounds possible? T开发者_开发知识库hanks!!


You can prevent the default functionality by passing the event:

$(document).bind('keydown', 'i', function(event) {
    event.preventDefault();
    $("input#foo").focus();
});

I imagine you'll want to prevent the keyup functionality too:

$(document).bind('keyup', 'i', function(event) {
    event.preventDefault();
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜