Definitive way to trigger keypress events with jQuery

后端 未结 10 2486
不知归路
不知归路 2020-11-22 00:33

I\'ve read all the answers on to this questions and none of the solutions seem to work.

Also, I am getting the vibe that triggering keypress with special characters

10条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 00:49

    The real answer has to include keyCode:

    var e = jQuery.Event("keydown");
    e.which = 50; // # Some key code value
    e.keyCode = 50
    $("input").trigger(e);
    

    Even though jQuery's website says that which and keyCode are normalized they are very badly mistaken. It's always safest to do the standard cross-browser checks for e.which and e.keyCode and in this case just define both.

提交回复
热议问题