简体   繁体   English

jQuery.trigger('click') 导致 IE8 中的“更改”事件

[英]jQuery.trigger('click') results in "change" Event in IE8

$('#myRadioButton').change(function(event) {
    var eventType = event.type;
    $(this).trigger('click'); // In IE8 only, this results in endless loop
});

The code snippet above results in a change event being fired in IE8 and hence an endless loop is started when my radio button is selected.上面的代码片段导致在 IE8 中触发更改事件,因此当我的单选按钮被选中时,将启动一个无限循环。 This is not the expected behaviour, as a 'click' event certainly is not a 'change' event.这不是预期的行为,因为“点击”事件当然不是“更改”事件。 In other browsers and IE9+ the code works as expected (ie, not an endless loop).在其他浏览器和 IE9+ 中,代码按预期工作(即,不是无限循环)。

The variable eventType equals "change" in IE8, both for initial, user triggered 'change' event, but also for the subsequent calls to itself.变量eventType在 IE8 中等于"change" ,对于初始的、用户触发的“change”事件,以及对自身的后续调用。

What is the reason for this behavior and how to stop IE8 from being a jerk and act like a normal browser?这种行为的原因是什么以及如何阻止 IE8 成为一个混蛋并像普通浏览器一样行事?

Best regards!此致!

Reason - no idea sorry.原因 - 不知道抱歉。

'Work Around' :- '解决':-

function radioChange() {
    $(this).off('change', radioChange);
    $(this).trigger('click');
    $(this).on('change', radioChange);
}

$('#myRadioButton').change(radioChange);

trigger is synchronous, so removing the change , then triggering the click , then re-attach the change should work. trigger是同步的,因此删除change ,然后触发click ,然后重新附加change应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM