有22行记录,每行记录对应一个checkbox,每行记录有帐户余额等信息,如果选中其中几条记录,怎么在页面上把被选中记录帐户总余额显示出来?
------解决方案--------------------
<script language= "javascript ">
function showData(){
var sum=0;
var obj=document.getElementsByName( "items "); //取得页面所有的items复选框对象
for(var i=0;i <obj.length;i++){
if(!obj[i].checked) continue; //如果没有选中,则执行下一次
sum+=parseFloat(obj[i].value); //如果被选中的话,则累加求和
}
alert( "总和为: "+sum);
}
</script>
<input type= "checkbox " value= "1 " name= "items "> 1 <br>
<input type= "checkbox " value= "2 " name= "items "> 2 <br>
<input type= "checkbox " value= "3 " name= "items "> 3 <br>
<input type= "checkbox " value= "4 " name= "items "> 4 <br>
<input type= "button " value= "打印 " onclick= "showData(); ">