calculate total value checkboxes jquery

夙愿已清 提交于 2019-12-18 04:17:18

问题


http://jsfiddle.net/kcd6r/

how do i calculate total rel value? if one checkbox selected the total will be updated automatically


回答1:


Hope this helps:

$("input[type=checkbox]").change(function(){
  recalculate();
});


function recalculate(){
    var sum = 0;

    $("input[type=checkbox]:checked").each(function(){
      sum += parseInt($(this).attr("rel"));
    });

    alert(sum);
}



回答2:


Same With JS :

    <script type="text/javascript">

    var total = 0;

    function test(item){
        if(item.checked){
           total+= parseInt(item.value);
        }else{
           total-= parseInt(item.value);
        }
        //alert(total);
        document.getElementById('Totalcost').innerHTML = total + " /-";
    }



    </script>
    </head>
    <body>

    <div id="container">
    <input type="checkbox" name="channelcost" value="10" onClick="test(this);"  />10<br />
    <input type="checkbox" name="channelcost" value="20" onClick="test(this);" />20 <br />
    <input type="checkbox" name="channelcost" value="40" onClick="test(this);" />40 <br />
    <input type="checkbox" name="channelcost" value="60" onClick="test(this);" />60 <br />
    </div>
    Total Amount : <span id="Totalcost"> </span>
    </body>
    </html>


来源:https://stackoverflow.com/questions/5066537/calculate-total-value-checkboxes-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!