【问题标题】:jQuery forward slash from button click按钮单击时的 jQuery 正斜杠
【发布时间】:2014-04-07 22:23:03
【问题描述】:

我试图做到这一点,以便当我单击按钮时输入一个键,我试图让/ 键工作。目前其代码值 (191) 正在输入 ¿。有什么想法吗?

$("button").click(function() {
  $("input").focus();
  var e = jQuery.Event("keydown");
  e.which = 191; // # Some key code value
  $("input").val(String.fromCharCode(e.which));
  $("input").trigger(e);
});

$('input').keydown(function(e){
  console.log('Yes keydown triggered. ' + e.which)
});

HTML:

<input type="text">
<button>button</button>

【问题讨论】:

标签: javascript jquery html keydown emulation


【解决方案1】:

http://jsfiddle.net/scottux/83GTf/

$("button").click(function() {
     $("input").focus();
    var e = jQuery.Event("keydown");
    e.which = 47; // # Some key code value
    $("input").val(String.fromCharCode(e.which));
    $("input").trigger(e);
});
$('input').keypress(function(e){
   console.log('Yes keydown triggered. ' + e.which)
});

keypress 事件使用正确的键码。

【讨论】:

    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    相关资源
    最近更新 更多