Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未选中复选框时如何显示0.00?_Javascript_Jquery_Checkbox - Fatal编程技术网

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

Javascript 未选中复选框时如何显示0.00?,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,如果未选中复选框,如何在totalcost字段中显示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)&l

如果未选中复选框,如何在totalcost字段中显示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>

游戏1(9.99)
游戏2(19.99)
游戏3(27.50)
第四场(45.65)
游戏5(87.20)
游戏5(87.20)
var clickHandlers=(函数(){ var form1=document.getElementById(“form1”), totalcost=document.getElementById(“totalcost”), //如果这始终是表单中的最后一个输入,我们可以避免再次使用 //总成本=表格1[form1.length-1]; 总和=0; form1.onclick=函数(e){ e=e | | window.event; var thisInput=e.target | | e.src元素; if(thisInput.nodeName.toLowerCase()='input'){ 如果(thisInput.checked){ sum+=parseFloat(thisInput.value); }否则{ if(thisInput.type.toLowerCase()=='复选框'){ sum-=parseFloat(thisInput.value); } } totalcost.value=(总和>0)?总和到固定值(2):“”; } } 返回null; }());
为输入指定默认的

<input type="text" id="totalcost" value="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;
}());

totalcost.value=(总和>0)?固定金额(2):“0.00”
需要与@martynas的解决方案一起使用。好的,当我选中复选框时,未选中的0.00将消失。好的,谢谢它的工作,我有一些问题,我如何在2位上显示0.00或和数?请澄清:“在2位上”?现在数字将显示在
中,但我想在
中显示总和数字,我该怎么做?如果我理解正确-您希望有两个输入显示完全相同的值?我的最后一个问题呢?如何使用数据设置
的宽度?
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;
}());