如何使用jQuery触发输入事件?

use*_*553 14 jquery triggers

我想在点击按钮时将'a'添加到输入值

这是我的代码(使用jQuery 1.4.4):

$("#button").click(function(){
    $("#input").trigger("focus");
    var e = jQuery.Event("keypress");
    e.which = '97';
    $("#input").trigger(e);
})
Run Code Online (Sandbox Code Playgroud)

然而,它似乎只是触发'焦点'事件,但未能'按键'.

小智 20

我用了:

$('#selector').val(quantity).trigger("input");
Run Code Online (Sandbox Code Playgroud)


Rei*_*gel 12

像这样??对不起,我对你的作品感到困惑..

$("#button").click(function(){
    $("#input").trigger("keypress") // you can trigger keypress like this if you need to..
    .val(function(i,val){return val + 'a';});
});
Run Code Online (Sandbox Code Playgroud)

reference:.val(function(index,value));