Ho un form del genere (uso checkbox, ma il discorso vale per qualunque input)
codice HTML:
<form name="myform" id="myform" method="POST" action="">
<input type="checkbox" name="mychecks[]" value="1" checked><br>
<input type="checkbox" name="mychecks[]" value="2" checked><br>
<input type="checkbox" name="mychecks[]" value="3" checked><br>
<input type="checkbox" name="mychecks[]" value="4" checked><br>
<input type="checkbox" name="mychecks[]" value="5" checked><br>
</form>
in questa situazione, posso "iterare" il mio array di input cosi':
codice:
var check_list = document.forms['myform'].elements[ 'mychecks[]' ];
for(check in check_list)
{
if (check_list[check].checked) console.log(check_list[check].value);
}
o selezionare un "singolo" elemento cosi':
codice:
console.log(check_list[3].value);
Ma se invece volessi usare un array associativo, tipo:
codice HTML:
<input type="checkbox" name="mychecks[primo]" value="1" checked><br>
<input type="checkbox" name="mychecks[secondo]" value="2" checked><br>
<input type="checkbox" name="mychecks[terzo]" value="3" checked><br>
<input type="checkbox" name="mychecks[quarto]" value="4" checked><br>
come posso accedere/iterare i campi??
il selettore "mychecks[]" non funziona!!!!