简体   繁体   中英

Is there a way to filter multiple 'key' events for jQuery event listener

While working with an API to search a database, I noticed I was getting back duplicate AJAX responses when two or more keys were pressed at once; ie, Ctrl+V to paste text into form. Is there a way to filter Control, Alt, or other key combinations that might occur?

$("#searchbar").on('keyup', function (e){
    // e.preventDefault();
    let value = e.target.value;

    $.ajax({
        url: 'api.domain.com/'+value,
        data: {}
    }).done(function (response_one){

        $("#serp").html(`
        <div class="response-container">
        ${response_one.param}
        </div>
        `);

        $.ajax({
            url: 'api.domain.com/feature'+value,
            data: {}
        }).done(function (response_two){
            response_two.method(function (args){

                $("#serp").append(`
                <div class="response-container">
                ${response_two.param}
                </div>
                `);     

            });

        });

    });

});});

Use https://api.jquery.com/keypress/ instead of Keyup. This is similar to the keydown event, except that modifier and non-printing keys such as Shift, Esc, and delete trigger keydown events but not keypress events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM