Why isn't backspace being detected using jquery keypress event?

前端 未结 3 2078
醉酒成梦
醉酒成梦 2020-12-30 00:42

Why isn\'t backspace being detected using jquery keypress event?

$(\"#field\").keypress(function(event){alert(event.which);});
3条回答
  •  爱一瞬间的悲伤
    2020-12-30 01:18

    The "keypress" event is (in its original IE form, and in Safari/Chrome) about an actual character being added to a text field. The backspace key clearly does not add a new character to the field value, so it causes no keypress event.

    Firefox and Opera are a little more willy-nilly about generating typing events.

    PPK (quirksmode) considers the IE and Webkit behaviors to be the sensible ones, and I pretty much agree.

提交回复
热议问题