我有这个javascript代码,从总和中减去一个复选框被选中时的值。在我来说,我每个复选框选中它substracts 20Javascript复选框的值
<script language="JavaScript">
function Calculate(){
var tag = window.document.getElementsByClassName("hsnb"), total = <? echo $total; ?>;
for (var i in tag){
total -= tag[i].checked && !isNaN(Number(20)) ? Number(20) : 0;
}
var cucu = + total.toFixed(2);
if(cucu < "20"){
alert("You dont have enough points!");
for(i = 1; document.getElementById("bifa" + i) !== null; i++) {
if (document.getElementById("bifa" + i).checked){
} else {
document.getElementById("bifa" + i).disabled = true;
}
}
}
window.document.getElementById("outputDiv").innerHTML = '<span style="font-size:20px;">You have: POINTS' + cucu + '</span>';
}
</script>
我想给每个复选框一定值时,让我们说:
<input type="checkbox" id="check" name="check" value="10">
<input type="checkbox" id="check" name="check" value="20">
如果我选中第一个复选框,它应该。减去10如果我检查第二个复选框减法20.你知道我的意思,减去每个复选框的值。你能帮助我吗?
我强烈建议使用一个好的JavaScript框架来处理这类工作,以使您的代码更具可读性并且更易于编写。我喜欢Dojo;很多人喜欢jQuery。你想收音机而不是复选框,或者可以应用多重减法?您可以向每个复选框添加一个onChange事件,该复选框会为每个更改减去(或返回)适当的金额。 – SAJ14SAJ