jquery select dropdown ignores keydown event when it's opened?

后端 未结 3 715
野趣味
野趣味 2020-12-20 20:44

I\'m trying to prevent backspace button to go one page back in every browser. For now I\'m using this code:

$(document).on(\"keydown\", function (e) {
    if         


        
3条回答
  •  [愿得一人]
    2020-12-20 21:20

    You'll have to check for keydown event on $(select) right now, if you add

    console.log(e.which)
    console.log(e.target)
    

    you'll notice that you won't get the keydown event when you click and press your keyboard while the select dropdown is active.

    This will make it for your

    $(document).on("keydown", $('select'), function (e) {
        if (e.which === 8) {
            e.preventDefault();
        }
    });
    

提交回复
热议问题