jQuery: keyup event for mobile device

后端 未结 4 1905
小鲜肉
小鲜肉 2020-11-28 14:30

I\'m having a few issues getting a keyup event to fire on my iPhone, my code is as follows:

        var passwordArray = [\"word\", \"test\", \"hello\", \"ano         


        
4条回答
  •  执笔经年
    2020-11-28 14:55

    There are three events you can use (but you have to be careful on how you "combine" them):

    • keyup : it works on devices with a keyboard, it's triggered when you release a key (any key, even keys that don't show anything on the screen, like ALT or CTRL);

    • touchend: it works on touchscreen devices, it's triggered when you remove your finger/pen from the display;

    • input: it's triggered when you press a key "and the input changes" (if you press keys like ALT or CTRL this event is not fired).

    The input event works on keyboard devices and with touchscreen devices, it's important to point this out because in the accepted answer the example is correct but approximative:

    test.on('keyup input', function(){
    }
    

    On keyboard based devices, this function is called twice because both the events keyup and input will be fired.

    The correct answer should be:

    test.on('keyup touchend', function(){
    }
    

    (the function is called on keyup for desktops/laptops OR on touchend for mobiles)

    or you can just simply use

    test.on('input', function(){
    }
    

    but remember that the input event will not be triggered by all keys (CTRL, ALT & co. will not fire the event).

提交回复
热议问题