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

查看:132
本文介绍了单选按钮和.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()

Use prop as seen here: .prop() vs .attr()

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


如果在jQuery中< 1.6:


If in 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);

请参阅小提琴: http://jsfiddle.net/maniator/6CDf3/
带有2个输入的示例: http://jsfiddle.net/maniator/6CDf3/2/

See fiddle: http://jsfiddle.net/maniator/6CDf3/
An example with 2 inputs appended: http://jsfiddle.net/maniator/6CDf3/2/

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

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