html keycodes not working in firefox

后端 未结 3 694
旧时难觅i
旧时难觅i 2021-01-21 18:59

I have the following code:

function noNumbers(e)
{
    var charCode = (e.which) ? e.which : 
                 ((e.charCode) ? e.charCode : 
                   ((         


        
3条回答
  •  执念已碎
    2021-01-21 19:14

    You can simply add console.log(e); in your function and debug what's going on:

    Chrome/IE doesn't call this function on Backspace and Delete key press. Firefox logs keypress { target: , key: "Backspace", charCode: 0, keyCode: 8 } and keypress { target: , key: "Delete", charCode: 0, keyCode: 46 } respectively. So there is two solutions:

    1) Add if for these keyCodes (8 and 46)

    2) Do not use keypress event and use keydown instead (as @Teemu wrote).

提交回复
热议问题