ho un questionario in html, dove le domande sono dei radio.
come posso sommare i valori dentro all'attributo value, di tutte le domande?
ho un questionario in html, dove le domande sono dei radio.
come posso sommare i valori dentro all'attributo value, di tutte le domande?
stranamente penso di esserci arrivato...
risultato: NaN.... che e'?codice:function calculateTotal() { result = (document.questions.one.value + document.questions.two.value + document.questions.three.value + document.questions.four.value + document.questions.five.value + document.questions.six.value + document.questions.seven.value + document.questions.eight.value + document.questions.nine.value + document.questions.ten.value + document.questions.eleven.value + document.questions.twelve.value + document.questions.thirteen.value + document.questions.fourteen.value + document.questions.fifthteen.value + document.questions.sixteen.value + document.questions.seventeen.value + document.questions.eighteen.value + document.questions.nineteen.value + document.questions.twenty.value); alert("Tot:"+result);
NaN = Not a Number
infatti i valori ritornati dai tuoi radio button saranno considerati come delle stringhe.
Prova a fare così:
codice:function calculateTotal() { var theRadio = document.form[nomeForm].elements.[nomeRadio] var intTotal = 0; for (i = 0; i < theRadio.length; i++) { intTotal += parseInt(theRadio[i].value); } window.alert("Tot: " + intTotal); }
ma.... questo devo ripeterlo per ogni opzione? (sono venti domande)
var theRadio = document.form[nomeForm].elements.[nomeRadio]
Scusami, avevo capito male il problema: pensavo che dovessi fare la somma dei valori di un unico radio e non di più radio...
Comunque la soluzione di usare parseInt() dovrebbe rimanere valida.
Secan
con i cicli non mi trovo, quindi ho provato cosi', ma nada
codice:function calculateTotal() { one = parseInt(document.questions.one.value); two = parseInt(document.questions.two.value); three = parseInt(document.questions.three.value); four = parseInt(document.questions.four.value); five = parseInt(document.questions.five.value); six = parseInt(document.questions.six.value); seven = parseInt(document.questions.seven.value); eight = parseInt(document.questions.eight.value); nine = parseInt(document.questions.nine.value); ten = parseInt(document.questions.ten.value); eleven = parseInt(document.questions.eleven.value); twelve = parseInt(document.questions.twelve.value); thirteen = parseInt(document.questions.thirteen.value); fourteen = parseInt(document.questions.fourteen.value); fifthteen = parseInt(document.questions.fifthteen.value); sixteen = parseInt(document.questions.sixteen.value); seventeen = parseInt(document.questions.seventeen.value); eighteen = parseInt(document.questions.eighteen.value); nineteen = parseInt(document.questions.nineteen.value); twenty = parseInt(document.questions.twenty.value); result = one + two + three + four + five + six + seven + eight + nine + ten + eleven + twelve + thirteen + fourteen + fifthteen + sixteen + seventeen + eighteen + nineteen + twenty; alert("Tot: " + result); }
Posta anche il codice HTML dei radio.
Di solito i radio sono delle collezioni (ci sono piu` campi con lo stesso nome), quindi quel codice non funziona.
Nuova politica di maggiore severita` sui titoli delle discussioni: (ri)leggete il regolamento
No domande tecniche in messaggi privati
questo si ripete per 20 domandecodice:<tr> <td width="246">Do you eat breakfast every day?</td> <td width="54"><input name="one" type="radio" value="1" /> Yes </td> <td width="44"><input name="one" type="radio" value="5" /> No </td> </tr> <tr> <td>Do you regularly eat on-the-go?</td> <td><input name="two" type="radio" value="5" /> Yes </td> <td><input name="two" type="radio" value="1" /> No </td> </tr> <tr> <td>Do you regularly skip meals?</td> <td><input name="three" type="radio" value="5" /> Yes </td> <td><input name="three" type="radio" value="1" /> No </td> </tr>
Ok ho capito.
inserisci questa funzione:Che poi chiami:codice:function valueradio (ogg) { for (var i=0; i<ogg.length; i++) { if (ogg[i].checked) return parseInt(ogg[i].value, 10); } }
one = valueradio(document.questions.one);
two = valueradio(document.questions.two);
...
Nuova politica di maggiore severita` sui titoli delle discussioni: (ri)leggete il regolamento
No domande tecniche in messaggi privati
cioe'... il submit chiama la funzione?
ok, fatto una ricerca piu' approfondita
http://forum.html.it/forum/showthrea...t=questionario