http://jsfiddle.net/kcd6r/
how do i calculate total rel value
? if one checkbox selected the total will be updated au开发者_开发技巧tomatically
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);
}
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>
精彩评论