怎么在循环里js做加法运算如:
var billmoney=document.getElementsByName("billmoney");
alert("billmoney.length======"+billmoney.length);
for(var j=0;j<billmoney.length;j++){
billfm.checkbillmoney.value=+billmoney[j].value;
//var testmoney+=billmoney[j].value;
}
要把billmoney[j].value相加!
var billmoney=document.getElementsByName("billmoney");
alert("billmoney.length======"+billmoney.length);
for(var j=0;j<billmoney.length;j++){
billfm.checkbillmoney.value=+billmoney[j].value;
//var testmoney+=billmoney[j].value;
}
要把billmoney[j].value相加!
<body>
<input name="billmoney" value="1" /><br />
<input name="billmoney" value="2" /><br />
<input name="billmoney" value="3" /><br />
<input name="billmoney" value="4" /><br />
<input name="billmoney" value="5" /><br />
<button onclick="buttonClick()">test</button>
<script type="text/javascript">
function buttonClick() {
var billmoney = document.getElementsByName("billmoney");
var sum = 0;
for (var j = 0; j < billmoney.length; j++) {
sum += parseFloat(billmoney[j].value);
}
alert(sum);
}
</script>
</body>
</html>
var billmoney=document.getElementsByName("billmoney");
alert("billmoney.length======"+billmoney.length);
var temp = 0;
for(var j=0;j <billmoney.length;j++){
temp += parseFloat(billmoney[j].value);
billmoney[j].value = temp;
//var testmoney+=billmoney[j].value;
}
billfm.checkbillmoney.value = temp;
billfm.checkbillmoney.value=+billmoney[j].value;
的作用是进行字符串的连接,因为billfm.checkbillmoney.value是字符串类型
<html>
<body>
<input type="checkbox" name="billmoney" value="1" /><br />
<input type="checkbox" name="billmoney" value="2" /><br />
<input type="checkbox" name="billmoney" value="3" /><br />
<input type="checkbox" name="billmoney" value="4" /><br />
<input type="checkbox" name="billmoney" value="5" /><br />
<button onclick="buttonClick()">test</button>
<script type="text/javascript">
function buttonClick() {
var billmoney = document.getElementsByName("billmoney");
var sum = 0;
for (var j = 0; j < billmoney.length; j++) {
sum += parseFloat(billmoney[j].value);
}
alert(sum);
}
</script>
</body>
</html>
<html>
<table>
<tr>
<td><input type="checkbox" name="a"></td>
<td><input type="text" name="billmoney" value="1"></td></tr><tr>
<td><input type="checkbox" name="a"></td>
<td><input type="text" name="billmoney" value="2"></td></tr><tr>
<td><input type="checkbox" name="a"></td>
<td><input type="text" name="billmoney" value="3"></td></tr>
</table>
<script type="text/javascript">
function buttonClick() {
var billmoney = document.getElementsByName("billmoney");
var sum = 0;
for (var j = 0; j < billmoney.length; j++) {
sum += parseFloat(billmoney[j].value);
}
alert(sum);
}
</script> </html>
要取到checkbox选上的的
<body>
<input type="checkbox" name="billmoney" value="1" /> <br />
<input type="checkbox" name="billmoney" value="2" /> <br />
<input type="checkbox" name="billmoney" value="3" /> <br />
<input type="checkbox" name="billmoney" value="4" /> <br />
<input type="checkbox" name="billmoney" value="5" /> <br />
<button onclick="buttonClick()">test </button>
<script type="text/javascript">
function buttonClick() {
var billmoney = document.getElementsByName("billmoney");
var sum = 0;
for (var j = 0; j < billmoney.length; j++) {
if (billmoney[j].checked)
sum += parseFloat(billmoney[j].value);
}
alert(sum);
}
</script>
</body>
</html>