简体   繁体   中英

how to check if checkbox is checked or not in IE 6

I am frustrated with the most idiotic browser of all time, for which, Bill Gates must be hanged I think.

I just want to check whether a checkbox is checked or not.

Somehow

 cbox.checked

property is always false. What other thing I can do. I saw all the other similar questions, but nothing is working with this stupid IE.

EDIT

I forgot to mention something that may be relevent. Html is like:

<input type='hidden' name='terms' value='0' /> 
<input type='checkbox' name='terms' id='terms' value='1' /> 

Hidden field is attached with it, because I am using Zend Form, and it always attaches a hidden field with every checkbox.

I am using protoype.js thats why I cannot use jQuery. I am checking that its check or not, in onsubmit event of the form. I guess somehow hidden field with the same name is tripping IE6

$('#myElement').is(":checked")

Ignore the IE6 nonsense, and just use jQuery.

http://www.jquery.com

Now you've posted your HTML your problem is clear: you've got two form elements with name 'terms' and IE 6 is finding the wrong one, because it has a broken implementation of document.getElementById that uses the name attribute of form elements as the id. The solution is to ensure you don't have form elements with the same name as any element you wish to refer to by id, and avoid using the same name for unrelated form elements.

Note that the problem will also exist in IE 7, according to this blog post .

I have to agree, IE 6 is a pain sometimes, and unfortunately we have to cater to that minority who still haven't upgraded. But this works for me within IE6

EDIT: After reading Tims Post have updated function reflect this. Tested and works in IE6.

function chk(){
        inps = document.getElementsByTagName("INPUT");
        for(i = 0; i < inps.length; i++){
         if(inps[i].name == "terms" && inps[i].type == "checkbox"){
          alert(inps[i].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