繁体   English   中英

未选中复选框时如何显示 0.00?

[英]How to show 0.00 when check box not checked?

未选中复选框时如何在总成本字段中显示 0.00?

<form id="form1">
<input type="checkbox" id='game0' value="9.99"  onclick="UpdateCost()">Game 1 ( 9.99)<br>
<input type="checkbox" id='game1' value="19.99" onclick="UpdateCost()">Game 2 (19.99)<br>
<input type="checkbox" id='game2' value="27.50" onclick="UpdateCost()">Game 3 (27.50)<br>
<input type="checkbox" id='game3' value="45.65" onclick="UpdateCost()">Game 4 (45.65)<br>
<input type="checkbox" id='game4' value="87.20" onclick="UpdateCost()">Game 5 (87.20)<br>
<input type="checkbox" id='game5' value="87.20" onclick="UpdateCost()">Game 5 (87.20)<br>    
<input type="text" id="totalcost" value="">
</form>

<script type="text/javascript">
var clickHandlers = (function () {
    var form1 = document.getElementById("form1"),
        totalcost = document.getElementById("totalcost"),
        // if this is always the last input in the form, we could avoid hitting document again with
        // totalcost  = form1[form1.length - 1];
        sum = 0;
        form1.onclick = function (e) {
            e = e || window.event;
            var thisInput = e.target || e.srcElement;
            if (thisInput.nodeName.toLowerCase() === 'input') {
                if (thisInput.checked) {
                    sum  += parseFloat(thisInput.value);
                }  else {
                    if (thisInput.type.toLowerCase() === 'checkbox') {
                        sum -= parseFloat(thisInput.value);
                    }
                }
                totalcost.value = (sum > 0) ? sum.toFixed(2) : "";
            }
    }
    return null;
}());
</script>

指定输入的默认value

<input type="text" id="totalcost" value="0.00">

将默认值设置为0.00

totalcost.value = (sum > 0) ? sum.toFixed(2) : "0.00";

演示

var clickHandlers = (function () {
    var form1 = document.getElementById("form1"),
        totalcost = document.getElementById("totalcost"),
// if this is always the last input in the form, we could avoid hitting document again with
// totalcost  = form1[form1.length - 1];
        sum = 0;
    form1.onclick = function (e) {
        e = e || window.event;
        var thisInput = e.target || e.srcElement;
        if (thisInput.nodeName.toLowerCase() === 'input') {
            if (thisInput.checked) {
                sum  += parseFloat(thisInput.value);
            }  else {
                if (thisInput.type.toLowerCase() === 'checkbox') {
                    sum -= parseFloat(thisInput.value);
                }
            }
            totalcost.value = (sum > 0) ? sum.toFixed(2) : "0.00";
        }
    }
    return null;
}());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM