简体   繁体   English

声明了 Function 但从未读取其值

[英]Function is declared but its value is never read

The function is working on the HTML page but not working from the external JS file and showing this error ('checkSelection' is declared but its value is never read). function 在 HTML 页面上工作,但无法从外部 JS 文件工作并显示此错误(已声明“checkSelection”,但从未读取其值)。 Is there any solution for this?有什么解决办法吗?

 function checkSelection(that) { if (that.value == "web-dev") { document.getElementById("web-section").style.display = "block"; } else { document.getElementById("web-section").style.display = "none"; } if (that.value == "grp-dsg") { document.getElementById("graphic-section").style.display = "block"; } else { document.getElementById("graphic-section").style.display = "none"; } if (that.value == "dgt-mkt") { document.getElementById("marketing-section").style.display = "block"; } else { document.getElementById("marketing-section").style.display = "none"; } }
 <input onchange="checkSelection(this);">

That is an error reported by the TypeScript checker in your IDE. To make it go away, this would be a workaround:这是 TypeScript 检查器在您的 IDE 中报告的错误。要使其远离 go,这将是一种解决方法:

window.checkSelection = function(that) {
    if (that.value == "web-dev") {
      document.getElementById("web-section").style.display = "block";
    } else {
      document.getElementById("web-section").style.display = "none";
    }
    if (that.value == "grp-dsg") {
      document.getElementById("graphic-section").style.display = "block";
    } else {
      document.getElementById("graphic-section").style.display = "none";
    }
    if (that.value == "dgt-mkt") {
      document.getElementById("marketing-section").style.display = "block";
    } else {
      document.getElementById("marketing-section").style.display = "none";
    }
  }

The basic problem though is your use of inline event listeners, which is widely considered bad practice.但是,基本问题是您使用内联事件侦听器,这被广泛认为是不好的做法。 Instead, I'd recommend you give that input an id and use addEventListener :相反,我建议您为该输入提供一个id并使用addEventListener

document.getElementById('section-picker').addEventListener('change', function() {
    if (this.value == "web-dev") {
      document.getElementById("web-section").style.display = "block";
    } else {
      document.getElementById("web-section").style.display = "none";
    }
    if (this.value == "grp-dsg") {
      document.getElementById("graphic-section").style.display = "block";
    } else {
      document.getElementById("graphic-section").style.display = "none";
    }
    if (this.value == "dgt-mkt") {
      document.getElementById("marketing-section").style.display = "block";
    } else {
      document.getElementById("marketing-section").style.display = "none";
    }
  });

If you take this advice, you need to make sure the Javascript executes after that input in the page is parsed.如果您接受此建议,则需要确保 Javascript 在页面中的input被解析执行。 The easiest way to achieve that is by applying the defer attribute in the script tag.实现这一点的最简单方法是在script标记中应用defer属性。

you need listen for the input first before using that in your function在 function 中使用之前,您需要先听取输入

 let number = document.querySelector('input'); function check(that){ if(that.value == 1){ console.log('hello'); } else{ console.log('bye'); } } number.addEventListener('input', ()=>{ check(number); });
 <input type="number">

take this for an example, you assign the input to a variable and then whenever that number is changed you run the function to check the value and display the result.以此为例,您将输入分配给一个变量,然后每当该数字发生更改时,您运行 function 来检查值并显示结果。 same thing goes for checkbox too.同样的事情也适用于复选框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM