简体   繁体   中英

Checking if the checkbox is checked :DOJO Javascript

I have a check box defined in my page for which the code is as follows,

<li class="Checkbox"> 
             <input 
                 id="Flag1" 
                 onclick="obj(this);"
                 type="checkbox"
                 name="Flag1.CheckName" 
                 dojotype="rmt.YNCheckBox"
                  <c:if test="${depotVal.checkFlag}">checked</c:if> 
             /> 
        </li>     

In my javascript, I am trying to check if the above checkbox is checked and only if it is checked I am continuing with the rest of the code.

I tried with the below code. It is not working and throwing an error at the end of page.

var chkbox1 = dijit.byId("Flag1");

alert(chkbox1.checked);  //returns true when checked and false when unchecked

if ((chkbox1.checked) || (chkbox1.checked == true)){
       ...
        }

Is there any other alternative ? How to make it work consistently? Also the above snippet does not work in Mozilla.

Where is it throwing an error? Is chkbox1 an actual dijit?

Should you be using the domNode of the dijit anyway?

chkbox1.domNode.checked

This is how I did it:

var t = query(".dijitCheckBoxInput");
array.forEach(t, function(item){
    if (item.checked) {
        item.checked = false;
    }
});

However this won't uncheck the checkboxes it will just change their internal state. To uncheck them I set the innerHTML of their container to checkBoxContainer.innerHTML = ' '; Then I executed the function that drew the checkboxes originally.

You probably figured it out by now but I figured I'd post it for anyone else trying to to something similar.

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