首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的函数不会将文本注入我的HTML中

我的函数不会将文本注入我的HTML中
EN

Stack Overflow用户
提问于 2022-09-26 16:10:50
回答 4查看 47关注 0票数 1

我正在为我的网站创建一个测试,我希望我的功能能够将文本注入到我的HTML中。它适用于所有其他功能,但这个功能不起作用。我试图改变我的注射方法。我也尝试过使用JQuery方法注入我的文本,但它仍然不允许我。

代码语言:javascript
复制
function quiz() {
  var f = 0;
  alert(f);
  if (document.getElementById("answer1").checked == true) {
    f++;
  };
  if (document.getElementById("answer2").checked == true) {
    f++;
  };
  if (document.getElementById("answer3").checked == true) {
    f++;
  };
  if (document.getElementById("answer4").checked == true) {
    f++;
  };

  function checkpass() {
    var a = ""
    if (f == 4) {
      a = "Y"
      alert(a)
    } else {
      a = "N"
      alert(a)
    };
  };
  checkpass();
  alert(document.getElementById("score"))
  document.getElementById("score").innerHTML = `Score:${f}/4 ${a}`;
};
代码语言:javascript
复制
<div id="WebBody">
  <P>Answer True or False on qustions</P>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer1" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer2" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer3" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer4" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <button onclick="quiz()">Submit</button>
  <p id="score">Score:Not Submitted</p>
</div>

EN

回答 4

Stack Overflow用户

发布于 2022-09-26 16:20:59

你的问题是:

代码语言:javascript
复制
document.getElementById("score").innerHTML = `Score:${f}/4 ${a}`;

您正在尝试访问变量"a“,即在checkpass函数中,您需要将该变量从校验函数放到外部。

这是你的代码

代码语言:javascript
复制
function checkpass() {
    var a = ""
    if (f == 4) {
      a = "Y"
      alert(a)
    } else {
      a = "N"
      alert(a)
    };
  };

应该是这样的:

代码语言:javascript
复制
var a = "";
function checkpass() {
    if (f == 4) {
      a = "Y"
      alert(a)
    } else {
      a = "N"
      alert(a)
    };
  };

顺便说一句,变量"f“是正确声明的,这不是问题,因为错误说"message": "Uncaught ReferenceError: a is not defined",变量"a”在变量"f“之后,所以它更像是不能访问变量"a”而不是var "f

票数 1
EN

Stack Overflow用户

发布于 2022-09-26 16:25:28

代码语言:javascript
复制
<!DOctyPE html>
<html lang="en">
<body>


<div id="WebBody">
  <P>Answer True or False on qustions</P>
  <p>Is ___</p>
              <form>
                    <input class="Radio-Button" id="answer1" name="T/F" type="radio">
                    <label>True</label><br>
                    <input class="Radio-Button" name="T/F" type="radio">
                    <label>False</label>
              </form> 
              <hr>
  <p>Is ___</p>
              <form>
                    <input class="Radio-Button" id="answer2" name="T/F" type="radio">
                    <label>True</label><br>
                    <input class="Radio-Button" name="T/F" type="radio">
                    <label>False</label>
              </form> 
              <hr>
  <p>Is ___</p>
              <form>
                    <input class="Radio-Button" id="answer3" name="T/F" type="radio">
                    <label>True</label><br>
                    <input class="Radio-Button" name="T/F" type="radio">
                    <label>False</label>
              </form> 
              <hr>
  <p>Is ___</p>
              <form>
                    <input class="Radio-Button" id="answer4" name="T/F" type="radio">
                    <label>True</label><br>
                    <input class="Radio-Button" name="T/F" type="radio">
                    <label>False</label>
              </form> 
              <hr>
      <button onclick="quiz()">Submit</button>
  <p id="score">Score:Not Submitted</p>
</div>




<script>
function quiz() {
  var f = 0;
  var a = ""
  alert(f);
  if (document.querySelector("#answer1").checked == true){
    f++ ;
  };
  if (document.querySelector("#answer2").checked == true){
    f++ ;
  };
  if (document.querySelector("#answer3").checked == true){
    f++ ;
  };
  if (document.querySelector("#answer4").checked == true){
    f++ ;
  };
  function checkpass(){
    if (f == 4) {
      a = "Y"
      alert(a)
    } else { 
      a = "N"
      alert(a)
    };
  };
  checkpass();
  alert(document.querySelector("#score").textContent);
  document.querySelector("#score").innerHTML = `Score:${f}/4 ${a}`;
};

quiz();
</script>
</body>
</html>
票数 0
EN

Stack Overflow用户

发布于 2022-09-26 16:28:10

您只需将a的定义移到checkpass之外,或者返回它,因为它在最后一行(checkpass之外)中是必需的。(我给后者看是因为另一个答案已经给出了第一个答案)。您可能需要查看devtools中的js调试器,因为这类错误并不少见,但很容易被调试器捕获。

(我还更改了您的“得分”元素警报,因为警报不像console.log --将不会显示完整、可扩展的对象,只显示字符串。)

代码语言:javascript
复制
function quiz() {
  var f = 0;
  alert(f);
  if (document.getElementById("answer1").checked == true) {
    f++;
  };
  if (document.getElementById("answer2").checked == true) {
    f++;
  };
  if (document.getElementById("answer3").checked == true) {
    f++;
  };
  if (document.getElementById("answer4").checked == true) {
    f++;
  };
  
  function checkpass() {
    var a = "";
    if (f == 4) {
      a = "Y"
      alert(a)
    } else {
      a = "N"
      alert(a)
    };
    return a;
  };
  var a = checkpass();
  alert(document.getElementById("score").outerHTML)
  document.getElementById("score").innerHTML = `Score:${f}/4 ${a}`;
};
代码语言:javascript
复制
<div id="WebBody">
  <P>Answer True or False on qustions</P>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer1" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer2" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer3" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <p>Is ___</p>
  <form>
    <input class="Radio-Button" id="answer4" name="T/F" type="radio">   <label>True</label><br>   <input class="Radio-Button" name="T/F" type="radio">   <label>False</label>
  </form>
  <hr>
  <button onclick="quiz()">Submit</button>
  <p id="score">Score:Not Submitted</p>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73856960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档