jQuery keyboard events

后端 未结 4 804
萌比男神i
萌比男神i 2020-11-29 13:31

Using jQuery, I would like to capture a keyboard event that is:

  • before the user lifts their finger from the key
  • after the characters
4条回答
  •  無奈伤痛
    2020-11-29 14:08

    You could listen on keydown event and store the value in a variable. That variable would have the value as it was before the new input, and the new input would be included in the keyup event

    UPDATE:

    Ok, I misunderstood your requirements, but there isn't an event that would meet your needs. The only thing I can think of to simulate this behaviour is the following:

    • listen on keydown/keypress
    • get the value from the event object (get event.which, then convert it to actual value)
    • use a variable like I mentioned in the original advice and concatenate the new input to it

    Here is a fiddle: http://jsfiddle.net/HpXuU/13/

    This is obviously not a perfect solution, as it needs some (one might argue unnecessary) work to get done right. I would advise to rethink your needs, but if this behavior is absolutely what you need, I think this is a step in the right direction.

提交回复
热议问题