php - Sum values with a check box/radio button -


i have database table of values 1 of columns displays cost of each asset.

i'm trying add checkboxes side of each of costs, can select values want , display averages , sum.

for example:

enter image description here

here sample html , js code should solve problem....

html blow

  <table width="100%" border="1">             <tr><th>cost</th><th>action</th></tr>             <tr>                 <td>30</td>                 <td><input type="checkbox" class="check_list" val="30"/></td>             </tr>             <tr>                 <td>20</td>                 <td><input type="checkbox" class="check_list" val="20"/></td>             </tr>             <tr>                 <td>25</td>                 <td><input type="checkbox" class="check_list" val="25"/></td>             </tr>             <tr>                 <td>35</td>                 <td><input type="checkbox" class="check_list" val="35"/></td>             </tr>             <tr><td style="text-align: right">sum</td><td><span id="sum"></span></td></tr>             <tr><td style="text-align: right">avg</td><td><span id="avg"></span></td></tr>         </table> 

& js blow

  <script>         $(document).ready(function(){             update();         });         $('input[type=checkbox]').click(function(){             update();         })         function update(){             var sum = 0;             $('.check_list').each(function () {                 if (this.checked) {                                   sum += number($(this).attr("val"));                  }             });             $("#sum").html(sum);             var checkcount = $(".check_list:checked").length;             $("#avg").html(parsefloat(sum/checkcount).tofixed(2));         }     </script> 

best of luck!!


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -