3 回答

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊
在您的 javascript 文件中,如果您創(chuàng)建一個(gè)名為 called 的變量total并將其放在方法之外,則您可以在每次運(yùn)行 checkTotal 時(shí)更新該值。
所以:
var total;
function checkTotal() {
var sum = 0;
for (i = 0; i < document.listForm.choice.length; i++) {
if (document.listForm.choice[i].checked) {
sum = sum + parseInt(document.listForm.choice[i].value);
}
}
total = sum;
}
function getTotal() {
return total;
}
然后在您的 html 中,您可以調(diào)用getTotal(),它將返回total設(shè)置的任何數(shù)字。

TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊
該函數(shù)傳遞哪個(gè)值?
無(wú),您的函數(shù)不會(huì)返回任何值,因此不會(huì)移交任何內(nèi)容。但是,如果您想返回任何值,可以通過(guò)該return語(yǔ)句來(lái)實(shí)現(xiàn)。
IE:
function checkTotal() {
var sum = 0;
document.listForm.total.value = '';
for (i = 0; i < document.listForm.choice.length; i++) {
if (document.listForm.choice[i].checked) {
sum = sum + parseInt(document.listForm.choice[i].value);
}
}
document.listForm.total.value = sum;
return sum;
}
所以當(dāng)你調(diào)用該函數(shù)時(shí)你可以保存它的返回值
喜歡 :
var total = checkTotal();
怎么稱呼?
目前它是使用事件偵聽(tīng)器屬性來(lái)調(diào)用的。IE。onChange
就像在 javascript 中這樣做一樣
document.querySelectorAll('input[type="checkbox"]')
.forEach(function(){
this.addEventListener("change", checkTotal)
})
如何在不破壞函數(shù)的情況下使 sum 成為全局變量?
您只需var sum = 0;在全局范圍內(nèi)聲明函數(shù)外部,如下所示
var sum = 0;
function checkTotal() {
sum = 0;
document.listForm.total.value = '';
for (i = 0; i < document.listForm.choice.length; i++) {
if (document.listForm.choice[i].checked) {
sum = sum + parseInt(document.listForm.choice[i].value);
}
}
document.listForm.total.value = sum;
}
javascript 中的任何函數(shù)也從其父函數(shù)繼承作用域,因此在聲明函數(shù)之前可用的任何內(nèi)容也可以在函數(shù)內(nèi)部使用(與 php 不同)。
但需要注意的是:使用letand聲明的變量const是塊作用域的。含義:無(wú)法從其直接封閉的外部訪問(wèn)它們{...}
將所有內(nèi)容放在一起并糾正一些錯(cuò)誤
最終代碼如下所示。
超文本標(biāo)記語(yǔ)言
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form name="list-form">
<table>
<tr>
<td>A</td>
<td>Inhalt 1</td>
<td><input type="checkbox" name="Inhalt_1" value="1"></td>
</tr>
<tr>
<td>B</td>
<td>Inhalt 2</td>
<td><input type="checkbox" name="Inhalt_2" value="1"></td>
</tr>
<tr>
<td>Total:</td>
<td colspan="2"><input disabled type="text" id="total" name="total" value="0" /></td>
</tr>
</table>
</form>
<script src="path/to/your/js/file.js" type="text/javascript"></script>
</body>
</html>
JS
var checkboxes = document.forms[0].querySelectorAll('input[type="checkbox"]'),
inpTotal = document.getElementById('total'),
sum = 0;
// first we define the checkTotal
function checkTotal() {
sum = 0;
checkboxes.forEach(el => {
if (el.checked) sum += +el.value;
});
inpTotal.value = sum;
// alert(sum);
}
// then we add the event listeners
checkboxes.forEach(el => el.addEventListener("change", checkTotal));
PS:最好的做法是盡可能將所有 javascript 與 html 放在單獨(dú)的文件中。

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
嘗試一下這個(gè)。
我知道這有點(diǎn)復(fù)雜,但這是很好的做法
我修復(fù)了您的非法 HTML 并將內(nèi)聯(lián)事件處理程序移至一個(gè) eventListener
我給了表單一個(gè) ID,使用名稱已過(guò)時(shí)且無(wú)用
如果您打算提交表單,則需要重命名其中一個(gè)復(fù)選框,或者如果您在服務(wù)器上使用 PHP,則添加到[]名稱以創(chuàng)建一個(gè)數(shù)組
在這里,我重命名了它們,并給了它們一個(gè)用于選擇器的類
document.getElementById("listForm").addEventListener("input", function() {
let sum = 0;
const checked = [...this.querySelectorAll(".choice:checked")].map(inp => +inp.value); // get all values from checked and convert to number
if (checked.length > 0) sum = checked.reduce((a, b) => a + b); // sum them
console.log(sum)
document.getElementById("total").value = sum; // show value
document.getElementById("showwhen2").classList.toggle("hide", sum < 2); // unhide when >= 2
});
.hide {
display: none;
}
<form id="listForm">
<table>
<tbody>
<tr id="A" +>
<td>A</td>
<td>Inhalt 1</td>
<td><input type="checkbox" class="choice" name="choiceA" value="1" /></td>
</tr>
<tr>
<td id="B">B</td>
<td>Inhalt 2</td>
<td><input type="checkbox" class="choice" name="choiceB" value="1" /></td>
</tr>
<tr>
<td>Summe:</td>
<td><input disabled type="text" size="2" name="total" id="total" value="0" /></td>
</tr>
</tbody>
</table>
</form>
<div id="showwhen2" class="hide">Equal 2</div>
添加回答
舉報(bào)