简体   繁体   中英

Javascript code to select check boxes not working in IE8

I've got the following code to that toggles selecting all checkboxes with a certain name.

The code works great in webkit based browsers but doesn't work at all in IE8.

Selecting toggle all, doesn't select anything.

<label class="checkbox">

<input name="product" type="checkbox" id="7553" value="true" title="option1">
option1
</label>

<label class="checkbox">

<input name="product" type="checkbox" id="65693" value="true" title="option2">
option2
</label>

<label class="checkbox">
<input type="checkbox" onClick="toggle(this)"><strong>Select all</strong>
</label>

Here's the JS

<script type="text/javascript">
function toggle(source) {
checkboxes = document.getElementsByName('product');
for(var i in checkboxes)
checkboxes[i].checked = source.checked;
}
</script>

Can anyone see why the above doesn't work in IE8?

Thanks

Probably because you should never use for..in on arrays (or, in this case, an array-like object).

Try this:

for( var i=0, l=checkboxes.length; i<l; i++) {
    checkboxes[i].checked = source.checked;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM