javascript IE8 & IE7 onchange 事件只有在重复选择后才会触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11068196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IE8 & IE7 onchange event is triggered only after repeated selection
提问by Registered User
I have a group of radio with an onchange handler:
我有一组带有 onchange 处理程序的收音机:
<input type="radio" name="Q12" value="radio" id="Q12_0" onchange="nextPnl('Q12');">
<br/>
<input type="radio" name="Q12" value="radio" id="Q12_1" onchange="nextPnl('Q12');">
?
function nextPnl(did)
{
document.write(did);
}?
The problem is that in IE8 & IE7, the onchange event is triggered only after repeated selection.
问题是在IE8 & IE7中,只有在重复选择后才会触发onchange事件。
Please view this demo in IE's Developer Tools [Browser Mode] IE8: http://jsfiddle.net/3zwur/2/
请在 IE 的开发者工具 [浏览器模式] IE8 中查看此演示:http: //jsfiddle.net/3zwur/2/
回答by F21
This is due to a bug with IE7 and IE8's change events. You should instead listen to the click
event.
这是由于 IE7 和 IE8 的更改事件的错误造成的。您应该改为监听click
事件。
As shown in this table on quirks mode, the change event on radio buttons and checkboxes is quite buggy in IE7 and IE8.
如这张关于 quirks mode 的表格所示,单选按钮和复选框上的更改事件在 IE7 和 IE8 中非常有问题。
You can listen to the click event like so:
您可以像这样收听点击事件:
<input type="radio" name="Q12" value="radio" id="Q12_0" onclick="nextPnl('Q12');">
<br>
<input type="radio" name="Q12" value="radio" id="Q12_1" onclick="nextPnl('Q12');">
And a fork of your fiddle: http://jsfiddle.net/T7VYL/
还有你的小提琴的一个叉子:http: //jsfiddle.net/T7VYL/
Usually, using a javascript library such as JQueryand YUImake your life easier, although from my testing, they do not fix this bug in older versions of IE.
通常,使用诸如JQuery和YUI 之类的 javascript 库会让您的生活更轻松,尽管从我的测试来看,它们并没有修复旧版本 IE 中的此错误。
If you would still like to listen to the change event, you can deploy this fix: http://www.ridgesolutions.ie/index.php/2011/03/02/ie8-chage-jquery-event-not-firing/. Basically it listens for the click event, and then causes the element to fire a change event.
如果您仍想收听更改事件,则可以部署此修复程序:http: //www.ridgesolutions.ie/index.php/2011/03/02/ie8-chage-jquery-event-not-firing/. 基本上它侦听单击事件,然后使元素触发更改事件。
As demonstrated by the asker's fiddle: http://jsfiddle.net/3zwur/3
正如提问者的小提琴所示:http: //jsfiddle.net/3zwur/3
回答by Carasel
Another option is to have an onchange event as you already have, and add an onclick event which removes focus from the radio button:
另一种选择是使用您已有的 onchange 事件,并添加一个 onclick 事件,该事件从单选按钮上移除焦点:
<input type="radio" name="Q12" value="radio" id="Q12_0" onclick="this.blur()" onchange="nextPnl('Q12');">
回答by Niek Nijland
Another solution is to put the onchange
event on the enclosing form via jQuery.
另一种解决方案是onchange
通过 jQuery将事件放在封闭的表单上。
$('#form').on('change', function() {
$(this).submit();
});