简体   繁体   中英

Trigger keypress on button click

$(document).keypress(function(event) {
    // +
    if (event.which == 43) {
        // ...
    }
}

HTML

<input type="button" value="+" name="plus">

How can I trigger the keypress method with + when clicking the button?

$('input[name="plus"]').click(function(){
    // ??? How to go further ???
})

Here you go

var e = jQuery.Event("keypress");
e.which = 43; // # Some key code value
$(document).trigger(e);

Src: Definitive way to trigger keypress events with jQuery

$(document).on('keypress',function(e) {
    if (e.keyCode == 43 || e.which == 43) {
        var identifier = e.target.id;
        console.log(e.target.id);
        $('#' + identifier).click();
    }
});

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