
|
|
checkbox.js |
| Simple script to check a checkbox-based form and redirect to one of 3 pages, depending on the selected values. The only requirement is that the checkboxes have a value of '0' (or something else false) if they should be unchecked and a value of '1' if they should be checked.
| |
| http://www.cloudmaster.com/~sauer/demos/checkbox.js
| |
function checkstuff(daForm){
// set these to the forwarded-to pages
var allright = "378_io_al-mp1_5.1.1.htm";
var somewrong = "378_io_al-mp1_5.1.2.htm";
// total number of right answers
var numright = 5;
// some alert texts
var alert_toofew = "Please select " + numright + " answers.";
var alert_toomany = "Please select " + numright + " answers.";
var alert_none = "Please select something.";
// stuff below here should remain unchanged
var numchecked = 0;
var gotright = false;
var gotwrong = false;
for(i=0; i<daForm.elements.length; i++){
daElem = daForm.elements[i];
if(daElem.type == "check"){
if(daElem.checked){
numchecked++;
if(daElem.value){
gotright = true;
}else{
gotwrong = true;
}
}
}
}
if(numchecked == numright && !gotwrong){
// all right:
document.location = allright;
}else if(numchecked > numright){
// too many checked:
alert(alert_toomany);
}if(numchecked > 0){
// checked some
if((gotright && !gotwrong) || (!gotright && gotwrong)){
// marked some right and none wrong or
// marked some wrong and none right
alert(alert_toofew);
}else if(gotwrong && gotright){
// marked some right and some wrong
document.location = somewrong;
}
}else{
// marked none.
alert(alert_none);
}
} | |
| [Append to This Answer] | |
| 2001-Nov-26 2:54pm |
| Previous: |
|
| Next: |
|
| ||||||||
| This FAQ administered by sauer@cloudmaster.com |