微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决单选按钮和.attr\'checked\',\'checked\'在IE7中不起作用

| 在IE7中添加附件后,是否有任何方法可以检查单选按钮? 似乎在每种浏览器中都可以正常工作,尽管我到处都在正确地阅读它,但看起来似乎无法在IE6,7中正常工作。我完全不知道为什么它不起作用。
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,但收音机实际上并没有被检查或被检查。 恶梦!其他人遇到这个问题并且有任何解决办法吗?     

解决方法

        如果在jQuery> = 1.6中: 使用
prop
,如下所示:.prop()与.attr()
$itemVariantRowRadio.prop(\'checked\',true);
如果在jQuery <1.6中:
$itemVariantRowRadio.attr(\'checked\',true);
也: 尝试像这样创建您的元素:
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/     ,        试试:
$itemVariantRowRadio.not(\':checked\').click().change();
因此,您实际上就像单击鼠标一样单击该复选框。
not(\':checked\')
将使您确定之前尚未对其进行检查。然后触发更改事件,因为jQuery click本身不会这样做。     ,        MSIE不允许您在创建
input
元素后更改其
type
。 参见http://bugs.jquery.com/ticket/1536和http://api.jquery.com/jQuery/#jQuery2 只需这样创建:
$(\'<input type=\"radio\">\')
    ,        如果将选中的属性附加到dom后再应用,则无需触发click事件: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);
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。