点击按钮时触发按键

问题描述 投票:5回答:2
$(document).keypress(function(event) {
    // +
    if (event.which == 43) {
        // ...
    }
}

HTML

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

单击按钮时如何使用+触发按键方法?

$('input[name="plus"]').click(function(){
    // ??? How to go further ???
})
javascript jquery keypress jquery-events
2个回答
8
投票

您去这里

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

源代码:Definitive way to trigger keypress events with jQuery


1
投票
$(document).on('keypress',function(e) {
    if (e.keyCode == 43 || e.which == 43) {
        var identifier = e.target.id;
        console.log(e.target.id);
        $('#' + identifier).click();
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.