$("input[name=checkbox]").bind("click",function(){ var result = 0; var chks=$("input[name=checkbox]:checked"); for(var i=0;i<chks.length;i++){ result+=parseFloat(chks[i].value); } $(".heji").text("合计:¥"+ result); }); var flag=0; $("input[name=selectallinput]").bind("click",function(){ if(flag == 0){ var result = 0; $("input[name=checkbox]").prop("checked","checked"); var chks=$("input[name=checkbox]:checked"); for(var i=0;i<chks.length;i++){ result+=parseFloat(chks[i].value); } $(".heji").text("合计:¥"+ result); flag = 1; }else if(flag == 1){ $("input[name=checkbox]").removeAttr("checked"); $(".heji").text("合计:¥0"); flag = 0; } });
先说说checkbox这个标签,不管你的checked=true or false,最后都会被选中,得出结论,仅仅要有checked这个属性,checked都会被选中。
我要实现这种功能,类似淘宝--->我的购物车--->能够选中若干个订单结账,以下的总价会随着你选的订单的价钱变化而变化。
$("input[name=checkbox]").bind("click",function(){ var result = 0; var chks=$("input[name=checkbox]:checked"); for(var i=0;i<chks.length;i++){ result+=parseInt(chks[i].value); } $(".heji").text("合计:¥"+ result); }); $("input[name=selectallinput]").bind("click",function(){ var result = 0; $("input[name=checkbox]").attr("checked","checked"); var chks=$("input[name=checkbox]:checked"); for(var i=0;i<chks.length;i++){ result+=parseInt(chks[i].value); } $(".heji").text("合计:¥"+ result); });