最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery trigger not working in IE. Why? - Stack Overflow

programmeradmin6浏览0评论
$('#XynBp0').find('input').each(function(){
    if ($(this).attr('value') == 'Cancel'){
        $(this).trigger('click');
    }
});

doesn't work in IE7

$('#XynBp0').find('input').each(function(){
    if ($(this).attr('value') == 'Cancel'){
        $(this).trigger('click');
    }
});

doesn't work in IE7

Share Improve this question edited Oct 14, 2010 at 23:06 Pekka 450k148 gold badges986 silver badges1.1k bronze badges asked Oct 14, 2010 at 22:51 user420095user420095 9
  • 1 What do you mean by "doesn't work". Does it find the input elements? What does $(this).attr('value') contain when you walk through each input element? Or is it the trigger that doesn't fire? – Pekka Commented Oct 14, 2010 at 22:54
  • AS i said in my title, "jQuery trigger" doesn't work. I'm not sure why but even though the element in question is found, the trigger doesn't fire. Either that or the fire does nothing. – user420095 Commented Oct 14, 2010 at 22:56
  • @syn4k so you know for a fact that the buttons are found in IE7, and the value = "Cancel" condition is met? You are 100% sure it's just the trigger that doesn't work? – Pekka Commented Oct 14, 2010 at 22:58
  • @Pekka, Yes, I am 100% certain. It's weird that even though the element is found the trigger fails. – user420095 Commented Oct 14, 2010 at 23:02
  • 3 @syn4k Also see this ment: api.jquery./trigger/#ment-48277488 Using .trigger('click') will not trigger the native click event. Maybe you'll need to use @Mouhannad's workaround – Pekka Commented Oct 14, 2010 at 23:05
 |  Show 4 more ments

4 Answers 4

Reset to default 2

it's strange but try to create a custom event

$('#XynBp0 input').bind('custom',function(){
 //code
})


$('#XynBp0').find('input').each(function(){
    if ($(this).attr('value') == 'Cancel'){
        $(this).trigger('custom');
    }
});

Does this work?

$.click() (or $.trigger('click')) doesn't simulate a mouse click; it fires off any onclick events bound to that element. If you haven't assigned an onclick event to that input you're searching for, nothing will happen.

It sounds like you're trying to submit the form with a traditional submit button (e.g. <input type="submit" value="Cancel">). If that's the case, you may have to use $(yourform).submit() to submit the form, in bination with some handling of the data sent to the server to simulate clicking the Cancel button.

Is it wrapped in a dom ready event? Might help if you provide more code.

$(function () {

    $('#XynBp0').find('input').each(function () {
        if ($(this).attr('value') == 'Cancel') {
            $(this).trigger('click');
        }
    });

});

Your code snippit doesn't make any sense, you are clicking inputs if they are canceled?

Here's some things to clean up in your code

$('input','#XynBp0').each(function () {
  var $this = $(this);
  if ( this.value === 'Cancel' ) { //Don't need jQuery here
    $this.trigger('click');  //Probably don't need it here either
  }
});

What does click even do? If you are trying to submit a form, use form.submit();

发布评论

评论列表(0)

  1. 暂无评论