英文:
How to read multiple value with checkboxes and autosum?
问题
我解决了自动求和的问题,但是当我选择1或2个项目或产品时,它会记录所有的产品。我该怎么办?
我期望能够根据我选择的项目名称或清单自动求和总金额。
HTML/PHP
<input type="hidden" class="test" name="request_test[]" onclick="autoSum(this);" i="" value="CBC">
<input type="checkbox" class="test" onclick="autoSum(this);" i="" value="50" data-price="50" >
<input type="hidden" class="test" name="request_test[]" onclick="autoSum(this);" i="" value="FBS">
<input type="checkbox" class="test" onclick="autoSum(this);" i="" value="200">
JavaScript
var total=0;
function autoSum(elem) {
if (elem.checked == true) {
total += Number(elem.value);
}else{
total -=Number(elem.value);
}
document.getElementById('amount').value = total.toFixed(0);
}
英文:
I solve the autosum but the problem is when I choose 1 or 2 item or product it will record all the product. What should I do?
I expected to autosum the total amount with item name or list I chose.
HTML/PHP
<input type="hidden" class="test" name="request_test[]" onclick="autoSum(this);" i="" value="CBC">
<input type="checkbox" class="test" onclick="autoSum(this);" i="" value="50" data-price="50" >
<input type="hidden" class="test" name="request_test[]" onclick="autoSum(this);" i="" value="FBS">
<input type="checkbox" class="test" onclick="autoSum(this);" i="" value="200">
JavaScript
var total=0;
function autoSum(elem) {
if (elem.checked == true) {
total += Number(elem.value);
}else{
total -=Number(elem.value);
}
document.getElementById('amount').value = total.toFixed(0);
}
答案1
得分: 2
你可以学习这个例子
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input type="checkbox" class="test" onClick="autoSum(this);" i="" value="50" data-price="50">
<input type="checkbox" class="test" onClick="autoSum(this);" i="" value="200">
<p id="amount">0</p>
</body>
</html>
Javascript
function autoSum(elem) {
var total=0;
var allCheckBoxes = document.querySelectorAll(".test");
allCheckBoxes.forEach(function(elem) {
if (elem.checked === true) {
total += Number(elem.value);
}
});
document.getElementById('amount').innerHTML = total;
}
演示链接在这里 https://jsbin.com/nedadawume/edit?html,js,console,output
英文:
You can study this example
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input type="checkbox" class="test" onClick="autoSum(this);" i="" value="50" data-price="50" >
<input type="checkbox" class="test" onClick="autoSum(this);" i="" value="200">
<p id="amount">0</p>
</body>
</html>
Javascript
function autoSum(elem) {
var total=0;
var allCheckBoxes = document.querySelectorAll(".test");
allCheckBoxes.forEach(function(elem) {
if (elem.checked === true) {
total += Number(elem.value);
}
});
document.getElementById('amount').innerHTML = total;
}
demo link here https://jsbin.com/nedadawume/edit?html,js,console,output
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论