单选按钮和 .attr('checked','checked') 在 IE7 中不起作用 [英] radio Buttons and .attr('checked','checked') does NOT work in IE7

查看:27
本文介绍了单选按钮和 .attr('checked','checked') 在 IE7 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 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);

<小时>

还有:

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

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/

这篇关于单选按钮和 .attr('checked','checked') 在 IE7 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆