在JS中单击复选框时显示总价

Display total price when checkbox clicked in JS

本文关键字:显示 复选框 JS 单击      更新时间:2023-09-26

我正在处理一个检查表单分配。所以,我有3个产品的价格和类型是复选框。合计字段将仅显示选中产品的总成本。但是,当我单击复选框时,合计字段将不显示任何内容。我是不是错过了什么?知道吗?非常感谢。我的HTML和JS:

    <form action="" id="theForm">
        <fieldset>
            <legend>
                Products
            </legend>
            <label>
                <input name="product" value="12.95" type="checkbox" id="p1" onclick="total()"/>
                Candy $12.95
            </label>
            <label>
                <input name="product" value="5.99" type="checkbox" id="p2" onclick="total()"/>
                Burger $5.99
            </label>
            <label>
                <input name="product" value="1.99" type="checkbox" id="p3" onclick="total()"/>
                Coke $1.99
            </label>
            <label>
                Total 
                <input value="$0.00" readonly="readonly" type="text" id="total"/>
            </label>
        </fieldset>
        <input value="Submit" type="submit"/>
        <input value="Reset" type="reset"/>
    </form>

JS-

function total() {
        var input = document.getElementById("form");
        var total = 0;
        for (var i = 0; i < input.length; i++) {
                if (input[i].checked) {
                        total += parseFloat(input[i].value);
                }
        }
        document.getElementById("total").value = "$" + total;
}

的三个问题

1:使用document.getElementsByName("product");
2:重命名函数。似乎使用total作为字段的ID会干扰相同名称的功能
3:四舍五入结果

function totalIt() {
  var input = document.getElementsByName("product");
  var total = 0;
  for (var i = 0; i < input.length; i++) {
    if (input[i].checked) {
      total += parseFloat(input[i].value);
    }
  }
  document.getElementById("total").value = "$" + total.toFixed(2);
}
 <form action="" id="theForm">
        <fieldset>
            <legend>
                Products
            </legend>
            <label>
                <input name="product" value="12.95" type="checkbox" id="p1" onclick="totalIt()"/>
                Candy $12.95
            </label>
            <label>
                <input name="product" value="5.99" type="checkbox" id="p2" onclick="totalIt()"/>
                Burger $5.99
            </label>
            <label>
                <input name="product" value="1.99" type="checkbox" id="p3" onclick="totalIt()"/>
                Coke $1.99
            </label>
            <label>
                Total 
                <input value="$0.00" readonly="readonly" type="text" id="total"/>
            </label>
        </fieldset>
        <input value="Submit" type="submit"/>
        <input value="Reset" type="reset"/>
    </form>

此行。。。

var input = document.getElementById("form");

只返回一个id为form的元素,而在您的示例中甚至不存在这样的元素。你要找的是…

var inputs = document.getElementsByName("product");

window.calculate = function(){
    var total = 0;
    var form = document.getElementById("theForm");
    var inputs = form.getElementsByTagName("input");
    
    for(var i = 0; i < inputs.length; i++){
        if(inputs[i].getAttribute("name") == "product" && inputs[i].checked)
            total += parseFloat(inputs[i].value);
    }
    alert(total);
}
<form action="" id="theForm">
        <fieldset>
            <legend>
                Products
            </legend>
            <label>
                <input name="product" value="12.95" type="checkbox" id="p1" onclick="calculate()"/>
                Candy $12.95
            </label>
            <label>
                <input name="product" value="5.99" type="checkbox" id="p2" onclick="calculate()"/>
                Burger $5.99
            </label>
            <label>
                <input name="product" value="1.99" type="checkbox" id="p3" onclick="calculate()"/>
                Coke $1.99
            </label>
            <label>
                Total 
                <input value="$0.00" readonly="readonly" type="text" id="total"/>
            </label>
        </fieldset>
        <input value="Submit" type="submit"/>
        <input value="Reset" type="reset"/>
    </form>

只需创建一个表单变量并添加一个事件侦听器,即可侦听和修改输入文本,如

function total_price(){
   let input = documnent.getElementsById('theForm'),
    total = 0.00,
    input = documnent.getElementsById('product');
    for (let i = 0; i <input.length; input++){
       if(input[i].checked){
         total += parseFloat(input[i].value)
       }
    }
    document.getElementById('total').value = total.toFixed(2)
}
document.getElementById('theForm').addEventListener('change', total_price)