How to trigger an input event with jQuery?

2020-03-09 07:18发布

I want to add 'a' to the value of input when click a button

Here is my code(with 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'.

7条回答
\"骚年 ilove
2楼-- · 2020-03-09 08:14

Why not just append to val()?

$("#button").click(function(){
  $("#input").val(
    $("#input").val() + "a"
  );
})
查看更多
登录 后发表回答