如何将javascript包含到项目复选框中,以便所选框自动计算价格值



显示要购买的四个或多个商品及其相应的购买价格(使用复选框可以选择要购买的商品(。为复选框指定名称item_0、item_1等。包括一个合计字段,当每个项目被选中/取消选中或指定数量发生变化时,该字段会自动对您的采购进行合计。为该字段提供名称合计。

我需要一些帮助。

这是迄今为止我能做的最简单的代码。

<h3> Cart </h3>
<input type = "checkbox" id = "item_0" name ="item_0" value = "Malt">
<label> Beta Malt </label>
<input type = "checkbox" id = "item_1" name ="item_1" value = "bread">
<label> A1 Bread </label>
<input type = "checkbox" id = "item_0" name ="item_2" value = "noodles">
<label> Indomie </label>
<input type = "checkbox" id = "item_0" name ="item_3" value = "milk">
<label> Cowbell </label>

首先,更改相同的属性名称。例如,您可以使用"如何使用JavaScript检查多个复选框?"?。使用这个想法。

var checkboxes = document.querySelectorAll('input[name="' + nameCheckbox + '"]:checked'), values = 0;
Array.prototype.forEach.call(checkboxes, function(el) {
values += parseInt(el.value);
});

警报(值(;

最新更新