在点击事件上触发按键 [英] trigger a keypress on click event

查看:163
本文介绍了在点击事件上触发按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码对我不起作用:

var e = jQuery.Event("keypress");

e.which = 43; // # Some key code value

$(document).trigger(e);

反正有虚拟地触发它吗?

Is there anyway to trigger it virtually?

$("input#genres").live('click', function(){

 var value = $(this).val();

 if($(this).is(':checked')) { 

 $("input.maininput").val(value);
 var e = jQuery.Event("keypress");

    e.which = 13; // # Some key code value

    $("input.maininput").trigger(e);

}

});

这是我的全部代码,但还是没有运气.

This is my whole code but still no luck.

推荐答案

您的代码正在运行,您只需要添加一些处理程序即可.

Your code is working, you just need to add some handler..

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

$(document).on('keypress', function(e) {
   alert(e.which);
});

$(document).trigger(e);​

这篇关于在点击事件上触发按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆