单击按钮时触发按键

时间:2011-07-01 13:50:42

标签: javascript jquery javascript-events keypress

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

HTML

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

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

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

2 个答案:

答案 0 :(得分:7)

你去吧

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

Src:Definitive way to trigger keypress events with jQuery

答案 1 :(得分: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();
    }
});