JavaScript if语句未执行

时间:2018-08-06 19:28:35

标签: javascript html

有人可以告诉我这里出了什么问题吗?如果if语句未执行,则代码是测验的一部分,每个问题和答案看起来都与所包含的html相似,该函数应针对获得多少百分比的内容创建摘要。

function CreateSummary() {
  var possiblePoints = 0;
  var claimedPoints=0;
  var intManual=0;
  for (var i=0;i < intTotalQuestions;i++) {
    var objQuestion = document.getElementById('divQuestion' + i);
    if (objQuestion == null) {
      alert('Null! ' + i); }
    console.log(objQuestion)
    for (var j=0;j<objQuestion.getElementsByTagName('input').length;j++) {
       var objCurrentAnswer = objQuestion.getElementsByTagName('input')[j];
       console.log(objCurrentAnswer)
       if (objCurrentAnswer.correct == "1" && objCurrentAnswer.checked) {
          console.log("add")
          claimedPoints += 1;  }
       if (objCurrentAnswer.correct == "1") {
         console.log("add")
         possiblePoints += 1;  }
       if (objQuestion.getElementsByTagName('textarea').length > 0) {
         intManual +=1;
        }
   }
 }
 console.log(possiblePoints)
 console.log(claimedPoints)
 document.getElementById('lblPercentage').innerHTML = 'Percentage: <strong>' + ((possiblePoints/claimedPoints)*100) + '</strong>';
}
<div id="divQuestion0"   style="width:100%;height::auto;align:center;background-color:lightyellow;display:block">
<div ><span>You should use the phrase"calm down" when dealing with an angry or upset individual. </span></div>
<div id="divAnswer0" style="text-align:left;width:100%;padding:20px;"
<span><input value="1" correct=0 id="Q0A0" name="rb0" type="radio" ></input> True</span><br />
<span><input value="2" correct=1 id="Q0A1" name="rb0" type="radio" ></input> False</span><br />
</div>
</div>

2 个答案:

答案 0 :(得分:0)

您需要执行CreateSummary,如果要在应用启动时调用该函数,可以使用 IIFE (立即调用函数表达式)(function CreateSummary() {...})()

答案 1 :(得分:0)

Correct不是输入的属性,因此在javascript中将不可用,并且objCurrentAnswer.correct始终为null。您可以这样做:

function CreateSummary() {
  var possiblePoints = 0;
  var claimedPoints=0;
  var intManual=0;
  for (var i=0;i < intTotalQuestions;i++) {
    var objQuestion = document.getElementById('divQuestion' + i);
    if (objQuestion == null) {
      alert('Null! ' + i); }
    console.log(objQuestion)
    for (var j=0;j<objQuestion.getElementsByTagName('input').length;j++) {
       var objCurrentAnswer = objQuestion.getElementsByTagName('input')[j];
       console.log(objCurrentAnswer)
       if (objCurrentAnswer.getAttribute("data-correct") == "1" && objCurrentAnswer.checked) {
          console.log("add")
          claimedPoints += 1;  }
       if (objCurrentAnswer.getAttribute("data-correct") == "1") {
         console.log("add")
         possiblePoints += 1;  }
       if (objQuestion.getElementsByTagName('textarea').length > 0) {
         intManual +=1;
        }
   }
 }
 console.log(possiblePoints)
 console.log(claimedPoints)
 document.getElementById('lblPercentage').innerHTML = 'Percentage: <strong>' + ((possiblePoints/claimedPoints)*100) + '</strong>';
}

您只能使用“ data-”作为前缀在html中添加自定义属性。

<div id="divQuestion0"   style="width:100%;height::auto;align:center;background-color:lightyellow;display:block">
<div ><span>You should use the phrase"calm down" when dealing with an angry or upset individual. </span></div>
<div id="divAnswer0" style="text-align:left;width:100%;padding:20px;"
<span><input value="1" data-correct="0" id="Q0A0" name="rb0" type="radio" ></input> True</span><br />-
<span><input value="2" data-correct="1" id="Q0A1" name="rb0" type="radio" ></input> False</span><br />
</div>
</div>