Salve a tutti, ho una funzione "f(frmObj)" che fa si che quando si seleziona un checkbox venga aggiunto il suo valore ad un campo di testo "who" (ogni valore è separato da un trattino "-").
Ho aggiunto però una funzione "check(field)" che permette di selezionare e deselezionare tutti i checkbox premendo un solo pulsante...
Vorrei fare in modo di richiamare la funzione "f(frmObj)" nella funzione "f(frmObj)"...
Cioè nell'esempio:
1)Al caricamento della pagina poichè tutte le checkbox sono selezionate, vorrei che "who" abbia il valore di tutte le checkbox.
2)Al "Seleziona Tutti" vorrei che "who" abbia il valore di tutte le checkbox.
3)Al "Deseleziona Tutti" vorrei che "who" abbia il valore "" (vuoto).
Questo è il codice che ho attualmente:
codice:
<SCRIPT LANGUAGE="JavaScript">
var checkflag = "true";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Deseleziona Tutti"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false;}
checkflag = "false";
return "Seleziona Tutti"; }
}
function f(frmObj)
{
var strVal = "";
var el = frmObj.elements;
var nFields = el.length;
var sepStr = "-";
for ( n = 0 ; n < nFields ; n++ )
{
if (el[n].type == "checkbox" && el[n].checked)
{
if (strVal.length > 0) strVal += sepStr;
strVal += el[n].value;
}
}
document.form1.who.value = strVal;
}
</script>
<form name="form1" method="post" action="test.php">
<input type=button value="Deseleziona Tutti" onClick="this.value=check(this.form.list)">
<input type=checkbox name=list value="1" checked onClick="f(this.form);">Opzione 1
<input type=checkbox name=list value="2" checked onClick="f(this.form);">Opzione 2
<input type=checkbox name=list value="3" checked onClick="f(this.form);">Opzione 3
<input type=checkbox name=list value="4" checked onClick="f(this.form);">Opzione 4
<input type=checkbox name=list value="5" checked onClick="f(this.form);">Opzione 5
<input type=text name=who>
<input type="submit" name="Submit" value="Invia">
</form>
Mi date una mano per favore?
Vi ringrazio tutti in anticipo!
Ciao!