jQuery中的按键和单击()没有结果

jQuery中的按键和单击()没有结果,jquery,ruby-on-rails,jquery-ui,jquery-plugins,Jquery,Ruby On Rails,Jquery Ui,Jquery Plugins,我正在尝试向我的rails页面添加键控件,这样当用户按空格键时,它会自动单击链接 #application.js $(document).ready(function(){ $(document).keypress(function(event) { if (event.which === 57) { $('#nextb').click(); } }); }); #in index.html <

我正在尝试向我的rails页面添加键控件,这样当用户按空格键时,它会自动单击链接

#application.js
      $(document).ready(function(){
    $(document).keypress(function(event) {
        if (event.which === 57) {
        $('#nextb').click();
        }
        }); 
     });

#in index.html
<a href="#" id="nextb" onclick="some other action">
#application.js
$(文档).ready(函数(){
$(文档).按键(功能(事件){
if(event.which==57){
$('#nextb')。单击();
}
}); 
});
#在index.html中

但它根本没有给我任何结果。我做错了什么?

它似乎对我很管用

但是您需要在
onclick
事件中添加实际代码

而且
57
键是键9,空格键是32


例如,在中,空格的
keyCode
应该是
32

if (event.which === 32) {
    $('#nextb').click();
}; 

演示

当用户按下空格键时,他试图触发点击。不要单击元素,然后检查空格。@Senica,答案是错误的,因为OP使用了错误的键代码。但我的回答并没有暗示你所说的。我只是建议使用jQuery绑定click处理程序,而不是通过onclick属性分配它。我的建议仍然有效,因为这是处理事件的正确方法。。