【发布时间】:2015-08-14 01:29:24
【问题描述】:
当试图确保在允许用户提交之前,我的测验中的每个问题(我正在做的)至少已经检查了单选按钮时,我在 if 语句中使用了一个 for 循环条件。代码如下:
HTML:
<form name="quizform">
<div id="quizpart1">
<h2>Title</h2>
<br>
<div id="notfinished1">
</div>
<br>
<!---Question 1: --->
<div class="row">
<div class="col-xs-8">
<label>1) Have you ever ...?</label>
</div>
<div class="col-xs-4">
<input type="radio" name="heartAttack?" value="true">Yes</input>
<br>
<input type="radio" name="heartAttack?" value="false">No</input>
</div>
</div>
我创建表单的 HTML 与上述其余问题的格式非常相似。
Javascript:
//Variables
var quizpart1names = ["heartAttack?", "familyHeartDisease?", "congenitalHeartDisease?", "lungDisease?", "currentHeartThyroidDisease?"];
//If Loop With For as a Conditional
if (
for (i=0; i < quizpart1names.length; i++){
$('input[name=quizpart1names[i]]:checked').length > 0;
}
){ //Used the jQuery Function :checked, and the logic behind the .length > 0 is that the "true" and "false" both have lengths > 0.
$("#quizpart1").hide();
//... The code goes on, but it works when there is no for loop in the conditional (when I was not checking if all the questions in the quiz I am making were answered.)
不幸的是,在 firefox 浏览器中,当我运行这个网页时,我写的任何 javascript(包括这个例子)都不再工作了。对此问题的任何见解都会有所帮助。
【问题讨论】:
-
真正的问题是什么?
-
@Popnoodles 在 Firefox 中,页面无法正确加载,基本上我编写的所有 javascript 都停止工作。
-
@Popnoodles 他有他的
for循环,条件应该进入if( /* condition */ ) -
您不能在条件中包含 for 循环。这是一个语法错误,并停止执行您的代码。你到底想实现什么?注意,
if不是循环,它是只执行一次的条件语句。 -
即使您的问题确实得到了解答,为了它在将来有用,您应该 edit 您的问题将您的代码转换为 minimal reproducible example 重现您面临的问题.
标签: javascript jquery html css radio-button