如何获得具有名为total
的类的所有值的总值<script>
function gettotal()
{
var total = 12;
}
</script>
<input type=text class=total value=5>
<input type=text class=total value=7>
<input type=submit name=submit value="Add" onclick=gettotal();>
答案 0 :(得分:3)
你可以这样做:
var total = 0;
var totalElems = document.getElementsByClassName("total");
for (var i = 0; i < totalElems.length; i++) {
total += parseInt(totalElems[i].value, 10);
}