如何使用 jQuery 触发输入事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15545557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trigger an input event with jQuery?
提问by user1990553
I want to add 'a' to the value of input when click a button
单击按钮时,我想将“a”添加到 input 的值中
Here is my code(with jQuery 1.4.4):
这是我的代码(使用 jQuery 1.4.4):
$("#button").click(function(){
$("#input").trigger("focus");
var e = jQuery.Event("keypress");
e.which = '97';
$("#input").trigger(e);
})
However, it seems only to trigger 'focus' event ,but failed to 'keypress'.
然而,它似乎只触发了'focus'事件,而未能'keypress'。
采纳答案by Reigel
like this?? Sorry, I'm confused with your writings..
像这样??对不起,我对你的作品感到困惑..
$("#button").click(function(){
$("#input").trigger("keypress") // you can trigger keypress like this if you need to..
.val(function(i,val){return val + 'a';});
});
reference: .val(function(index, value));
回答by Luminita Balas
I used:
我用了:
$('#selector').val(quantity).trigger("input");
回答by Grant Thomas
Why not just append to val()
?
为什么不直接附加到val()
?
$("#button").click(function(){
$("#input").val(
$("#input").val() + "a"
);
})
回答by user1990553
I have known how to deal with it.
我知道如何处理它。
Add a eventListener on keypress event to the input and use val to change the value.
将 keypress 事件上的 eventListener 添加到输入并使用 val 更改值。
In this way there is no need to trigger focus event.
这样就不需要触发焦点事件了。
$("#button").click(function(){
var e = jQuery.Event("keypress");
e.chara = 'a';
$("#input").trigger(e);
});
$("#input").keypress(function(e){
$(this).val(e.chara);
})
回答by Arun P Johny
According to the documentation
根据文档
Although .trigger() simulates an event activation, complete with a synthesized event object, it does not perfectly replicate a naturally-occurring event.
尽管 .trigger() 模拟事件激活,并带有合成的事件对象,但它并不能完美地复制自然发生的事件。
so the best you could do is
所以你能做的最好的就是
$("#button").click(function(){
$("#input").trigger("focus").val($("#input").val() + 'a');
})
回答by Flint O'Brien
In case you need to take into account the current cursor and text selection...
如果您需要考虑当前光标和文本选择...
This wasn't working for me for an AngularJS app on Chrome. Someone pointed out the trigger event will not make the character visible in the input field (at least, that's what I was seeing). In addition, the previous solutions don't take into account the current cursor position and text selection in the input field. I had to use a wonderful library jquery-selection.
这不适用于 Chrome 上的 AngularJS 应用程序。有人指出触发事件不会使字符在输入字段中可见(至少,这是我看到的)。此外,之前的解决方案没有考虑输入字段中的当前光标位置和文本选择。我不得不使用一个很棒的库jquery-selection。
I have a custom on-screen numeric keypad that fills in multiple input fields. I had to...
我有一个自定义的屏幕数字小键盘,可以填充多个输入字段。我不得不...
- On focus, save the lastFocus.element
On blur, save the current text selection (start and stop)
var pos = element.selection('getPos') lastFocus.pos = { start: pos.start, end: pos.end}
When a button on the my keypad is pressed:
lastFocus.element.selection( 'setPos', lastFocus.pos) lastFocus.element.selection( 'replace', {text: myKeyPadChar, caret: 'end'})
- 在焦点上,保存 lastFocus.element
在模糊时,保存当前的文本选择(开始和停止)
var pos = element.selection('getPos') lastFocus.pos = { start: pos.start, end: pos.end}
当我的键盘上的按钮被按下时:
lastFocus.element.selection( 'setPos', lastFocus.pos) lastFocus.element.selection( 'replace', {text: myKeyPadChar, caret: 'end'})
回答by bipen
you don't need keypress or any other event of input just use val
.. and focus it...
您不需要按键或任何其他输入事件,只需使用val
.. 并将其聚焦...
try this
尝试这个
$("#button").click(function(){
$("#input").val('a').focus();
})