Trigger a keypress/keydown/keyup event in JS/jQuery?

后端 未结 9 2060
长情又很酷
长情又很酷 2020-11-22 07:10

What is the best way to simulate a user entering text in a text input box in JS and/or jQuery?

I don\'t want to actually put text in the input box,

9条回答
  •  伪装坚强ぢ
    2020-11-22 07:46

    I thought I would draw your attention that in the specific context where a listener was defined within a jQuery plugin, then the only thing that successfully simulated the keypress event for me, eventually caught by that listener, was to use setTimeout(). e.g.

    setTimeout(function() { $("#txtName").keypress() } , 1000);
    

    Any use of $("#txtName").keypress() was ignored, although placed at the end of the .ready() function. No particular DOM supplement was being created asynchronously anyway.

提交回复
热议问题