简体   繁体   中英

Show/hide based on checkbox

I have code to see if any checkboxes are checked, it's working. Here's where I'm usure, after it sees that none are checked, checked I want to have a DIV show up (hide/show I guess) somwhere and disappear if any of the boxes are checked.


function checkBoxValidate(cb) {
for (j = 0; j < 8; j++) {
if (eval("document.myform.checkbox[" + j + "].checked") = false) {
document.views.checkbox[j].checked = false;
      }
   }
}

----------


<form name="myform">
                      <input class="checkbox" name="1" type="checkbox" value="check_1" onclick="document.getElementById('r-click').innerHTML = '1;" id="click">
                      1
                      <input class="checkbox" name="2" type="checkbox" value="check_2" onclick="document.getElementById('s-click').innerHTML = '2';" id="click">
                      2
                     </form>

<div id=showInstructions>Check boxes</div>

Thanks!

Added a few lines of code to your original code to demonstrate:

(btw, the whole "eval" thing is kinda messy and could be written much simpler...)

function checkBoxValidate(cb) {
var somethingIsChecked = false;
for (j = 0; j < 8; j++) {
if (eval("document.myform.checkbox[" + j + "].checked") = false) {
    document.views.checkbox[j].checked = false;
}
else {
    somethingIsChecked = true;
}

if (somethingIsChecked)
    document.getElementById("showInstructions").style.display = "block";
else
    document.getElementById("showInstructions").style.display = "none";
function checkBoxValidate(cb) {
    var noneChecked = true;
    for (j = 0; j < 8; j++) {
        if (eval("document.myform.checkbox[" + j + "].checked") == false) {
            document.views.checkbox[j].checked = false;
        } else {
            noneChecked = false;
        }
    }
    document.getElementById("showInstructions").style.display = noneChecked ? "block" : "none";
}

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