JavaScript 函数的返回值

我对 HTML 和 JavaScript 还很陌生。我有一个表格,我可以在其中单击复选框。JavaScript 函数正在计算选中框的总和,并在 html 文本字段中给出结果。到目前为止,一切都很好。


现在我想隐藏代码或不隐藏代码,无论结果低于2还是更高,但我不知道我可以使用哪个值来检查(在脚本和html中)。

该函数传递哪个值?怎么称呼?

如何在不破坏函数的情况下使 sum 成为全局变量?


我的代码:


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;

}

//alert ("Summe: " + ???);

<table>

  <form name="listForm">

    <tr>

      <td>A</td>

      <td>Inhalt 1</td>

      <td><input type="checkbox" name="choice" value="1" onchange="checkTotal()" /></td>

    </tr>

    <tr>

      <td>B</td>

      <td>Inhalt 2</td>

      <td rowspan="2" ;> <input type="checkbox" name="choice" value="1" onchange="checkTotal()" /></td>

    </tr>

    <tr>

      <td>Summe:</td>

      <td><input disabled type="text" size="2" name="total" value="0" /></td>

    </tr>

  </form>

</table>


眼眸繁星
浏览 128回答 3
3回答

侃侃尔雅

在您的 javascript 文件中,如果您创建一个名为 called 的变量total并将其放在方法之外,则您可以在每次运行 checkTotal 时更新该值。所以:var total;function checkTotal() {&nbsp; var sum = 0;&nbsp; for (i = 0; i < document.listForm.choice.length; i++) {&nbsp; &nbsp; if (document.listForm.choice[i].checked) {&nbsp; &nbsp; &nbsp; sum = sum + parseInt(document.listForm.choice[i].value);&nbsp; &nbsp; }&nbsp; }&nbsp; total = sum;}function getTotal() {&nbsp; return total;}然后在您的 html 中,您可以调用getTotal(),它将返回total设置的任何数字。

米琪卡哇伊

该函数传递哪个值?无,您的函数不会返回任何值,因此不会移交任何内容。但是,如果您想返回任何值,可以通过该return语句来实现。IE:function checkTotal() {&nbsp; var sum = 0;&nbsp; document.listForm.total.value = '';&nbsp; for (i = 0; i < document.listForm.choice.length; i++) {&nbsp; &nbsp; if (document.listForm.choice[i].checked) {&nbsp; &nbsp; &nbsp; sum = sum + parseInt(document.listForm.choice[i].value);&nbsp; &nbsp; }&nbsp; }&nbsp; document.listForm.total.value = sum;&nbsp; return sum;}所以当你调用该函数时你可以保存它的返回值喜欢 :var total = checkTotal();怎么称呼?目前它是使用事件侦听器属性来调用的。IE。onChange就像在 javascript 中这样做一样document.querySelectorAll('input[type="checkbox"]')&nbsp; &nbsp; .forEach(function(){&nbsp; &nbsp; &nbsp; &nbsp; this.addEventListener("change", checkTotal)&nbsp; &nbsp; })如何在不破坏函数的情况下使 sum 成为全局变量?您只需var sum = 0;在全局范围内声明函数外部,如下所示var sum = 0;&nbsp;function checkTotal() {&nbsp; sum = 0;&nbsp; document.listForm.total.value = '';&nbsp; for (i = 0; i < document.listForm.choice.length; i++) {&nbsp; &nbsp; if (document.listForm.choice[i].checked) {&nbsp; &nbsp; &nbsp; sum = sum + parseInt(document.listForm.choice[i].value);&nbsp; &nbsp; }&nbsp; }&nbsp; document.listForm.total.value = sum;}javascript 中的任何函数也从其父函数继承作用域,因此在声明函数之前可用的任何内容也可以在函数内部使用(与 php 不同)。但需要注意的是:使用letand声明的变量const是块作用域的。含义:无法从其直接封闭的外部访问它们{...}将所有内容放在一起并纠正一些错误最终代码如下所示。超文本标记语言<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <title>Document</title></head><body>&nbsp; &nbsp; <form name="list-form">&nbsp; &nbsp; &nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>A</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Inhalt 1</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="checkbox" name="Inhalt_1" value="1"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>B</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Inhalt 2</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="checkbox" name="Inhalt_2" value="1"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Total:</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td colspan="2"><input disabled type="text" id="total" name="total" value="0" /></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; </form>&nbsp; &nbsp; <script src="path/to/your/js/file.js" type="text/javascript"></script></body></html>JSvar checkboxes = document.forms[0].querySelectorAll('input[type="checkbox"]'),&nbsp; &nbsp; inpTotal = document.getElementById('total'),&nbsp; &nbsp; sum = 0;// first we define the checkTotalfunction checkTotal() {&nbsp; &nbsp; sum = 0;&nbsp; &nbsp; checkboxes.forEach(el => {&nbsp; &nbsp; &nbsp; &nbsp; if (el.checked) sum += +el.value;&nbsp; &nbsp; });&nbsp; &nbsp; inpTotal.value = sum;&nbsp; &nbsp; // alert(sum);}// then we add the event listenerscheckboxes.forEach(el => el.addEventListener("change", checkTotal));PS:最好的做法是尽可能将所有 javascript 与 html 放在单独的文件中。

阿波罗的战车

尝试一下这个。我知道这有点复杂,但这是很好的做法我修复了您的非法 HTML 并将内联事件处理程序移至一个 eventListener我给了表单一个 ID,使用名称已过时且无用如果您打算提交表单,则需要重命名其中一个复选框,或者如果您在服务器上使用 PHP,则添加到[]名称以创建一个数组在这里,我重命名了它们,并给了它们一个用于选择器的类document.getElementById("listForm").addEventListener("input", function() {&nbsp; let sum = 0;&nbsp; const checked = [...this.querySelectorAll(".choice:checked")].map(inp => +inp.value); // get all values from checked and convert to number&nbsp; if (checked.length > 0) sum = checked.reduce((a, b) => a + b); // sum them&nbsp; console.log(sum)&nbsp; document.getElementById("total").value = sum; // show value&nbsp; document.getElementById("showwhen2").classList.toggle("hide", sum < 2); // unhide when >= 2});.hide {&nbsp; display: none;}<form id="listForm">&nbsp; <table>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; <tr id="A" +>&nbsp; &nbsp; &nbsp; &nbsp; <td>A</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>Inhalt 1</td>&nbsp; &nbsp; &nbsp; &nbsp; <td><input type="checkbox" class="choice" name="choiceA" value="1" /></td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td id="B">B</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>Inhalt 2</td>&nbsp; &nbsp; &nbsp; &nbsp; <td><input type="checkbox" class="choice" name="choiceB" value="1" /></td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>Summe:</td>&nbsp; &nbsp; &nbsp; &nbsp; <td><input disabled type="text" size="2" name="total" id="total" value="0" /></td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </tbody>&nbsp; </table></form><div id="showwhen2" class="hide">Equal 2</div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript