开发者

How to use Jquery to check checkbox states and do something based on conditions?

开发者 https://www.devze.com 2023-01-31 18:41 出处:网络
Here is my setup. <input type=\"checkbox\" name=\"item1\" value=\"100\" class=\"100\" /> <input type=\"checkbox\" name=\"item2\" value=\"200\" class=\"200\" />

Here is my setup.

<input type="checkbox" name="item1" value="100" class="100" />
<input type="checkbox" name="item2" value="200" class="200" />
<input type="checkbox" name="item3" value="300" class="300" />

<p>$0.00</p>

The criteria I need is: change P tag to the amount selected in the checkbox if more than 1 checkbox is checked, add the amount together and display in P tag if 1 or more checkbox is unchecked, subtract the amount and 开发者_如何学Cdisplay in P tag if none is selected or all of them are unchecked, display ZERO in the P tag.

I only have this so far but obviously it only displays the price of that selected box and doesn't add nor does it remove when I uncheck the box.

$("input[type=checkbox]").click(function() {
var amount = $(this).attr("class");
$("p").html("$"+amount);
});

Any help appreciated!


simple try...

$("input[type=checkbox]").change(function() {
    var amount = 0;
    $("input[type=checkbox]:checked").each(function(){
        amount += parseFloat(this.value,10);
    });
    $("p").html("$"+amount);
});

Crazy demo

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号