Very very basic question but I've got the following HTML:
<input type="text" id="nspcc" value="0" onkeyup="doTotal();" />
<input type="text" id="barnados" value="0" onkeyup="doTotal();" />
<input type="text" id="savethechildren" value="0" onkeyup="doTotal();" />
<input type="text" id="childresnsociety" value="0" onkeyup="doTotal();" />
<input type="text" id="childreninneed" value="0" onkeyup="doTotal();" />
<input type="text" id="total" disabled="disabled" />
And the following jQuery:
function doTotal() {
var one,two,three,four,five,total;
one = $('#nspcc').val();
two = $('#barnados').val();
three = $('#savethechildren').val();
four = $('#childresnsociety').val();
five = $('#childreninneed').val();
total = one+two+three+four+five;
$('#total').val(total);
}
doTotal();
I'm probably doing something daft but why does total
concatenate instead of adding the values? Do I need to use parseInt
or something?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…