javascript - 单击按钮时的 jQuery 正斜杠

标签 javascript jquery html keydown emulation

我试图做到这一点,以便当我单击按钮时输入一个键,我试图让 / 键工作。当前其代码值 (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>

最佳答案

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)
});

按键事件使用正确的键码。

关于javascript - 单击按钮时的 jQuery 正斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22924204/

相关文章:

javascript - 单击复选框或其标签时的淡出和淡入 Div

javascript - 分析事件代码显示在源代码中,但不显示在控制台中

javascript - 使用 javascript 更改输入值 - 出了什么问题?

php - jquery 中的 .get()

html - CSS重叠div

javascript - jquery onclick提交和jquery函数

javascript - Bootstrap JS 未加载

javascript - 我怎样才能确保这个 CSS 过渡动画

javascript - 以表格格式使用 ng-repeat

javascript - 如何在有背景颜色的情况下一张一张地粘标题?