开发者

Issue with jQuery setting checkboxes as checked in IE6 (not showing as checked?)

开发者 https://www.devze.com 2022-12-25 03:39 出处:网络
The Code: var lstInstanceIds = getData.lstInstanceIds.s开发者_开发技巧plit(\',\'); for(var i=0; i<lstInstanceIds.length; i++) {

The Code:

var lstInstanceIds = getData.lstInstanceIds.s开发者_开发技巧plit(',');
    for(var i=0; i<lstInstanceIds.length; i++) {
        var value = lstInstanceIds[i];
        $('input[value=' + value + ']').attr('checked','checked');
    }

So all i'm doing is looping a list and setting the attribute checked as checked where the values meet.

This works fine in Chrome, Firefox, IE7/8, Safari. But not in IE6...


Use attr('checked', true) instead.


IETester is not an accurate representation of a true IE6 environment. Mainly due to using the javascript engine that is in the local machine rather than the IE6 one. I have come across many pages that work and look great in IE6 using IETester but fail horribly when brought up on a real IE6 on Windows XP environment. My suggestion there is to get the free Virtual PC image of IE6 on WinXP SP3. It is very useful in finding real errors using jQuery (though it is still a pain to debug).


I was able to setup a basic example based solely on what you mentioned here (so hopefully I'm not too far off), and it worked testing in IETester with a 6 instance.

<input type="checkbox" id="chk1" value="chk1" />
<input type="checkbox" id="chk2" value="chk2" />
<input type="checkbox" id="chk3" value="chk3" />
<input type="checkbox" id="chk4" value="chk4" />
<input type="checkbox" id="chk5" value="chk5" />

var ids = "chk1,chk3,chk5";
var lstInstanceIds = ids.split(',');
for (var i=0; i<lstInstanceIds.length; i++) {
    var value = lstInstanceIds[i];
    $('input[value=' + value + ']').attr('checked','checked');
}


I've had a similar problem before. To fix it, I used code similar to this:

var lstInstanceIds = getData.lstInstanceIds.split(',');
for(var i=0; i<lstInstanceIds.length; i++) {
    var value = lstInstanceIds[i];
    var checkbox = $('input[value=' + value + ']').get(0);
    if(checkbox.checked == false)
        checkbox.click();
}

This checks whether the checkbox is checked. If not, it clicks it, thereby checking it. Should support all browsers!

0

精彩评论

暂无评论...
验证码 换一张
取 消