Javascript if语句不执行
有人能告诉我这里出了什么问题吗?如果语句没有执行,则代码是测验的一部分,其中每个问题和答案看起来都像包含的html,函数应该创建一个分数百分比的摘要Javascript if语句不执行,javascript,html,Javascript,Html,有人能告诉我这里出了什么问题吗?如果语句没有执行,则代码是测验的一部分,其中每个问题和答案看起来都像包含的html,函数应该创建一个分数百分比的摘要 函数CreateSummary(){ var possiblePoints=0; var claimedPoints=0; var intManual=0; 对于(var i=0;i
函数CreateSummary(){
var possiblePoints=0;
var claimedPoints=0;
var intManual=0;
对于(var i=0;i'+((可能的点/claimdpoints)*100)+'';
}
在与愤怒或不安的人打交道时,你应该使用“冷静”一词。
你需要执行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'+((可能的点/claimdpoints)*100)+'';
}
您只能使用“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()在另一个未显示的函数中调用。