Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript if语句不执行_Javascript_Html - Fatal编程技术网

Javascript if语句不执行

Javascript if语句不执行,javascript,html,Javascript,Html,有人能告诉我这里出了什么问题吗?如果语句没有执行,则代码是测验的一部分,其中每个问题和答案看起来都像包含的html,函数应该创建一个分数百分比的摘要 函数CreateSummary(){ var possiblePoints=0; var claimedPoints=0; var intManual=0; 对于(var i=0;i

有人能告诉我这里出了什么问题吗?如果语句没有执行,则代码是测验的一部分,其中每个问题和答案看起来都像包含的html,函数应该创建一个分数百分比的摘要

函数CreateSummary(){
var possiblePoints=0;
var claimedPoints=0;
var intManual=0;
对于(var i=0;i';
}

在与愤怒或不安的人打交道时,你应该使用“冷静”一词。

你需要执行
CreateSummary
,如果你想在应用程序启动时调用函数,你可以使用IIFE(立即调用的函数表达式)
(函数CreateSummary(){…})(

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>';
}
函数CreateSummary(){
var possiblePoints=0;
var claimedPoints=0;
var intManual=0;
对于(var i=0;i';
}
您只能使用“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>

在与愤怒或不安的人打交道时,你应该使用“冷静”一词。

在哪里调用函数?在哪里定义了
intTotalQuestions
?如果不是,那么循环体将不会执行。我非常确定,除非html发生了某些变化,否则您不能在html标记上添加自定义属性,例如使用“correct”属性。这没有意义。。。如果他在应用程序启动时调用该函数,它将永远不会知道用户是否标记了正确的
无线电
,而且循环也永远不会发生(也不会嵌套
if
语句),因为
intTotalQuestions
没有设置在任何位置,因此它没有值,他也可以使用
数据-
,不仅仅是ng-
,他的主要问题不是这个。因为在他的示例中,他从不调用
CreateSummary()
,并且他在
for
中使用的变量未声明
ng-
由Angular使用。对于常规HTML中的自定义属性,应使用
data-
。请参见我在自定义属性中添加了引号。CreateSummary()在另一个未显示的函数中调用。