单选按钮和 .attr('checked','checked') 在 IE7 中不起作用中不、单选、按钮、attr

2023-09-06 19:19:31 作者:男神的身上有wifi

有没有办法在 IE7 中附加时检查单选按钮?

Is there any way to get radio buttons checked upon appending in IE7?

似乎在每个浏览器中都有效,但在 IE6,7 中似乎并不有效,尽管到处阅读我都在正确地做.我完全不知道为什么它不起作用.

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.

var $itemVariantRowRadio = $("<input/>")
    .attr("type", "radio")
    .attr("name", "itemvariant")
    .addClass("itemvariant")
    .val('whatever');


    $itemVariantRowRadio.attr('checked', 'checked');
    $itemVariantRow.append($itemVariantRowRadio)

现在如果我在 IE6/7 中执行 console.log($itemVariantRowRadio.attr('checked') 那么它说它设置为 TRUE 但收音机实际上并没有得到检查或拿起检查.

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.

噩梦!还有其他人遇到过这个问题并有任何解决方法吗?

Nightmare! Anyone else come across this and have any sort of fix?

推荐答案

如果在 jQuery >= 1.6:

If in jQuery >= 1.6:

使用 prop 如下所示:.prop() vs .attr()

$itemVariantRowRadio.prop('checked', true);

如果在 jQuery <1.6:

If in jQuery < 1.6:

$itemVariantRowRadio.attr('checked', true);

HTML中的单选按钮为什么总有一个处于选定状态

还有:

尝试像这样创建你的元素:

Try creating your element like so:

var $itemVariantRowRadio = $("<input/>",{
     type: 'radio',
     name: 'itemvariant',
     class: 'itemvariant',
     checked: true,
     value: 'whatever'
});
$itemVariantRow.append($itemVariantRowRadio);

参见小提琴:http://jsfiddle.net/maniator/6CDf3/附加 2 个输入的示例:http://jsfiddle.net/maniator/6CDf3/2/

 
精彩推荐
图片推荐