codice:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pagina vuota</title>
<script type="text/javascript">
function anyCheck(oField) {
for (var iChBx = 0, nCount = 0, sName = oField.name, oForm = oField.form, nChBxs = oForm[sName].length; iChBx < nChBxs; iChBx++) {
if (oForm[sName][iChBx].checked) { nCount++; }
}
return nCount;
}
</script>
</head>
<body onload="document.playlist.total.value=anyCheck(document.playlist['ckbox[]'][0]);">
<form method="post" name="playlist" action="#">
<fieldset>
<legend>Conta checkbox</legend>
<input type="checkbox" name="ckbox[]" value="tizio" onclick="this.form.total.value=anyCheck(this);" id="tuoId1" /> <label for="tuoId1">primo valore</label>
<input type="checkbox" name="ckbox[]" value="caio" onclick="this.form.total.value=anyCheck(this);" id="tuoId2" /> <label for="tuoId2">secondo valore</label>
<input type="checkbox" name="ckbox[]" value="sempronio" onclick="this.form.total.value=anyCheck(this);" id="tuoId3" /> <label for="tuoId3">etc. etc.</label>
Numero di checkbox selezionate: <input type="text" name="total" size="2" readonly /></p>
<input type="submit" value="Count Checkboxes"></p>
</fieldset>
</form>
</body>
</html>