开发者

Trigger keypress on button click

开发者 https://www.devze.com 2023-03-16 22:28 出处:网络
$(document).keypress(function(event) { // + if (event.which == 43) { // ... } } HTML <input type=\"button\" value=\"+\" name=开发者_开发技巧\"plus\">
$(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();
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号