0

我怎样才能得到所有有一个类的值的总值总计

<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();>
4

1 回答 1

3

你可以做:

var total = 0;
var totalElems = document.getElementsByClassName("total");
for (var i = 0; i < totalElems.length; i++) {
    total += parseInt(totalElems[i].value, 10);
}
于 2013-11-04T20:44:14.630 回答