HTML--JS 多列求和

时间:2024-03-08 13:43:29
 1 <html>
 2     <head>
 3         <title>多列求和</title>
 4         <script type="text/javascript">
 5             function addAll(){
 6                 var total_value=0;
 7                 for(i=0;i<document.myform.check.length;i++){
 8                     if(document.myform.check[i].checked){
 9                         total_value+= parseInt( document.myform.money[i].value );
10                     }
11                 }    
12                 alert(total_value);                
13                 document.getElementById("total").value = total_value;
14                             
15             }
16             
17             
18             
19         </script>
20     
21     </head>
22     <body>
23         <form action="" method="post" name="myform">
24             <table border="1" cellpadding="0" cellspacing="0" width="20%" height="40%">
25                 <tr>
26                     <td>金额</td>
27                     <td>是否求和</td>
28                 </tr>
29                 <tr>
30                     <td><input type="text" name="money" /></td>
31                     <td><input type="checkbox" name="check" /></td>
32                 </tr>
33                 <tr>
34                     <td><input type="text" name="money" /></td>
35                     <td><input type="checkbox" name="check" /></td>
36                 </tr>
37                 <tr>
38                     <td><input type="text" name="money" /></td>
39                     <td><input type="checkbox" name="check" /></td>
40                 </tr>
41                 <tr>
42                     <td><input type="text" name="money" /></td>
43                     <td><input type="checkbox" name="check" /></td>
44                 </tr>
45                 <tr>
46                     <td><input type="button" value="求和" onclick="addAll()" /></td>
47                     <td><input type="text" id="total" readonly /></td>    
48                 </tr>
49                 
50                 
51             </table>
52             
53         </form>
54         
55     </body>
56 </html>