简体   繁体   English

模拟被按下的键盘上的按钮,将该键的值添加到文本输入中

[英]Simulate a button on the keyboard being pressed, add the value of that keypress to text input

My objective is to be able to run a function that simulates a key being pressed on the keyboard, and then the value of that key being inserted into an input field. 我的目标是能够运行一个函数,该函数模拟在键盘上按下的键,然后将该键的值插入到输入字段中。 My reasoning for this is that I am using a js framework that automatically filters data based off of the input but it only filters the data when the input is typed in. Using document.getElementById('search_box').value will obviously populate the input with text. 我这样做的原因是,我使用的是一个js框架,该框架会根据输入自动过滤数据,但仅在输入输入时才过滤数据。使用document.getElementById('search_box').value显然会填充输入与文本。 But doing this doesn't trigger the data to be filtered because the input text technically isn't typed in. So I was wondering if it was possible to run a function that would basically simulate keys being pressed and then inserting the value of those keys into an input field. 但是这样做并不会触发数据过滤,因为从技术上来说,没有输入输入文本。所以我想知道是否有可能运行一个基本上模拟被按下的键然后插入这些键的值的函数输入字段。

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

According to filter.js's main repo's Issues #154 : Can I trigger a filter programatically with jquery like $("#filterid").val("bla").change() ? 根据filter.js的主存储库的问题#154我可以使用$ {“#filterid”)。val(“ bla”)。change()这样的jquery以编程方式触发过滤器吗?

$("#filterid").trigger('change') will work. $("#filterid").trigger('change')将起作用。 It also depend upon events: change, click etc 它还取决于事件:更改,点击等

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

In the example page , I used $('#searchbox').val("father").trigger('keyup') to add "father" to search and trigger a filter effect. 示例页面中 ,我使用$('#searchbox').val("father").trigger('keyup')添加“ father”来搜索并触发滤镜效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM