radio Buttons and .attr('checked','checked') does NOT work in IE7

二次信任 提交于 2019-11-30 08:43:44
Neal

If in jQuery >= 1.6:

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

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

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

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

Try: $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.

MSIE doesn't allow you to change the type of an input element once it has been created.

See http://bugs.jquery.com/ticket/1536 and http://api.jquery.com/jQuery/#jQuery2

Just create it so:

$('<input type="radio">')

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/

<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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!