jQuery 单选按钮和 .attr('checked','checked') 在 IE7 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6432246/
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
radio Buttons and .attr('checked','checked') does NOT work in IE7
提问by Intellix
Is there any way to get radio buttons checked upon appending in IE7?
有没有办法在 IE7 中附加时检查单选按钮?
What seems to work in every browser, doesn't look like it works in IE6,7 despite reading everywhere that I'm doing it correctly. I have absolutely no idea why it's not working.
似乎在每个浏览器中都有效的东西,尽管在任何地方都读到了我正确地做的事情,但它在 IE6,7 中看起来并不像。我完全不知道为什么它不起作用。
var $itemVariantRowRadio = $("<input/>")
.attr("type", "radio")
.attr("name", "itemvariant")
.addClass("itemvariant")
.val('whatever');
$itemVariantRowRadio.attr('checked', 'checked');
$itemVariantRow.append($itemVariantRowRadio)
Now if I do a console.log($itemVariantRowRadio.attr('checked')
in IE6/7 then it says that it's set to TRUE but the radio doesn't actually get checked or pick up as checked.
现在,如果我console.log($itemVariantRowRadio.attr('checked')
在 IE6/7 中执行 a ,那么它会说它设置为 TRUE,但实际上并没有检查无线电或接收到已检查的无线电。
Nightmare! Anyone else come across this and have any sort of fix?
恶梦!其他人遇到过这个问题并有任何修复方法吗?
回答by Naftali aka Neal
If in jQuery >= 1.6:
如果在 jQuery >= 1.6 中:
Use prop
as seen here: .prop() vs .attr()
使用prop
如下所示:.prop() vs .attr()
$itemVariantRowRadio.prop('checked', true);
If in jQuery < 1.6:
如果在 jQuery < 1.6 中:
$itemVariantRowRadio.attr('checked', true);
ALSO:
还:
Try creating your element like so:
尝试像这样创建你的元素:
var $itemVariantRowRadio = $("<input/>",{
type: 'radio',
name: 'itemvariant',
class: 'itemvariant',
checked: true,
value: 'whatever'
});
$itemVariantRow.append($itemVariantRowRadio);
See fiddle: http://jsfiddle.net/maniator/6CDf3/
An example with 2 inputs appended: http://jsfiddle.net/maniator/6CDf3/2/
请参阅小提琴:http
: //jsfiddle.net/maniator/6CDf3/附加 2 个输入的示例:http: //jsfiddle.net/maniator/6CDf3/2/
回答by sod
Try: $itemVariantRowRadio.not(':checked').click().change();
尝试: $itemVariantRowRadio.not(':checked').click().change();
So you actually click the checkbox just like you would do as a user with the mouse. not(':checked')
will get you sure it was not already checked before. And trigger the change event afterwards, as jQuery click does not do that by itself.
因此,您实际上就像使用鼠标一样单击复选框。not(':checked')
会让你确定它之前没有被检查过。并在之后触发更改事件,因为 jQuery click 本身不会这样做。
回答by Alnitak
MSIE doesn't allow you to change the type
of an input
element once it has been created.
MSIE不允许你改变type
了的input
,一旦它已经创建的元素。
See http://bugs.jquery.com/ticket/1536and http://api.jquery.com/jQuery/#jQuery2
请参阅http://bugs.jquery.com/ticket/1536和http://api.jquery.com/jQuery/#jQuery2
Just create it so:
只需创建它:
$('<input type="radio">')
回答by scrappedcola
You don't need to trigger the click event if you apply the checked attribute after you append it to the dom: http://jsfiddle.net/XjHUe/2/
如果在将其附加到 dom 后应用已选中的属性,则不需要触发单击事件:http: //jsfiddle.net/XjHUe/2/
<div id='content'></div>
var $itemVariantRowRadio = $("<input/>")
.attr("type", "radio")
.attr("name", "itemvariant")
.addClass("itemvariant")
.val('whatever');
//$itemVariantRowRadio.attr("checked", true);
$("#content").append($itemVariantRowRadio);
$(".itemvariant").attr('checked',true);
var val = $(".itemvariant").attr("checked");
alert(val);