Definitive way to trigger keypress events with jQuery

后端 未结 10 2312
不知归路
不知归路 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:58

    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. As Nadia points out in the original comments, the character is never visible in the input field (at least, that was my experience). In addition, the previous solutions don't take into account the current text selection in the input field. I had to use a wonderful library jquery-selection.

    I have a custom on-screen numeric keypad that fills in multiple input fields. I had to...

    1. On focus, save the lastFocus.element
    2. On blur, save the current text selection (start and stop)

      var pos = element.selection('getPos')
      lastFocus.pos = { start: pos.start, end: pos.end}
      
    3. When a button on the my keypad is pressed:

      lastFocus.element.selection( 'setPos', lastFocus.pos)
      lastFocus.element.selection( 'replace', {text: myKeyPadChar, caret: 'end'})
      

提交回复
热议问题