【问题标题】:Simulate a button on the keyboard being pressed, add the value of that keypress to text input模拟按下键盘上的按钮,将该按键的值添加到文本输入中
【发布时间】:2017-07-05 19:40:00
【问题描述】:

我的目标是能够运行一个函数来模拟在键盘上按下的键,然后将该键的值插入到输入字段中。我的理由是我正在使用一个 js 框架,它会根据输入自动过滤数据,但它只在输入输入时过滤数据。使用document.getElementById('search_box').value 显然会用文本填充输入。但是这样做不会触发要过滤的数据,因为在技术上没有输入输入文本。所以我想知道是否可以运行一个基本上模拟按键然后插入这些键的值的函数进入输入字段。

function populateField() {
    document.getElementById('search_box').value = "mr"; //this needs to be changed to insert the value via keyboard
    document.getElementById('search_box').focus();
}

【问题讨论】:

  • 请显示所有相关代码(HTML、CSS、JavaScript)。
  • 您使用的是哪个框架?框架应该有一个 API 允许你在没有键盘事件的情况下触发它。

标签: javascript html


【解决方案1】:

根据 filter.js 的主要 repo 的Issues #154我可以使用 jquery 以编程方式触发过滤器,例如 $("#filterid").val("bla").change() 吗?

$("#filterid").trigger('change') 可以。它还取决于事件:更改、点击等

$('#searchbox').val("father").trigger('keyup')

example page中,我使用$('#searchbox').val("father").trigger('keyup')添加“父亲”进行搜索,触发滤镜效果。

【讨论】:

  • @AaronHorne 以下适用于示例页面:jiren.github.io/filter.js/index.html$('#searchbox').val("father").trigger('keyup')
  • 是的,这绝对有效,也很有意义。感谢您的帮助
  • @AaronHorne 欢迎您。请将此标记为正确答案。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多