jquery trigger event

2020-05-01 07:30发布

How can we call the trigger click event on a live object.

$("#continue").live("keypress",function(){
 if (e.which == 32 || e.which == 13) {
    $(this).trigger("click");
  }
});

when i press enter on the button it is coming into if block but it is not triggering the button. can u please help me in this. Thanks in advance.

3条回答
Emotional °昔
2楼-- · 2020-05-01 08:16

You need to check if the $(this) really matches your button.

Try replacing :

$(this).trigger('click')

With

$('#continue').trigger('click')
查看更多
该账号已被封号
3楼-- · 2020-05-01 08:28

You use the argument "e", but never actually pass it in as an argument.

$("#continue").live("keypress",function(){

needs to be:

$("#continue").live("keypress",function(e){
查看更多
ゆ 、 Hurt°
4楼-- · 2020-05-01 08:29

Try this:

$("#continue").live("keyup",function(e){
 if (e.keyCode == 32 || e.keyCode == 13) {
    $(this).trigger("click");
  }
});

I covered this in this post a bit: EnterKey is not working sometimes in IE8, using jQuery's keyPressed

But basically there are "Special Keys" that won't fire on keypress and you have to use keydown or keyup.

Example here: http://jsfiddle.net/uS4XK/1/

Though I have to be honest this is a strange behavior to want. You want a click event on #continue when they press enter in it?

查看更多
登录 后发表回答