I have 3 checkboxes in a view. When anyone of those is checked, I want to add 3 to the value of a javascript variable named cost. If anyone of the checkboxes is then unchecked, I want to subtract 3 from the value of cost.
Currently my code always subtracts 3 when a checkbox is checked or unchecked. How can I add 3 when a checkbox is checked?
HTML
<div class="panel-heading">Checkboxes</div>
<div id="chkbox" class="panel-body">
<div>
@Html.CheckBoxFor(m => m.IsT)
@Html.LabelFor(m => m.IsT, "T")
</div>
<div>
@Html.CheckBoxFor(m => m.IsS)
@Html.LabelFor(m => m.IsS, "S")
</div>
<div>
@Html.CheckBoxFor(m => m.IsR)
@Html.LabelFor(m => m.IsR, "R")
</div>
</div>
jQuery
var cost = @Session["Cost"];
$('#chkbox').change(function (chk) {
debugger;
if ($(chk).prop("checked")){
cost = cost + 3;
} else {
cost = cost- 3;
}
$("#product").val(cost);
$("#spProduct").text(cost);
});
$(chk)? that would return undefined and you will always execute theifblockif ($(this).is(':checked') {- but you have not even indicated what the element withid="chkbox"ischkgives me the selected checkbox.