Javascript 为什么在我单击submit按钮后,click事件处理程序执行了这么多次?
我试图用Javascript编写一个简单的测验程序来完成我的网络作业,但我遇到了一个问题,我只点击了一次按钮,但点击处理函数一直在执行 它们附在按钮上,用于放置“提交”,当我选择正确答案a、b、c时,它可以正常工作。但当我选择答案时,a、a、a、click事件处理程序会多次触发 我在网上找到了答案,他们建议使用off(),但它似乎不起作用 这是htmlJavascript 为什么在我单击submit按钮后,click事件处理程序执行了这么多次?,javascript,Javascript,我试图用Javascript编写一个简单的测验程序来完成我的网络作业,但我遇到了一个问题,我只点击了一次按钮,但点击处理函数一直在执行 它们附在按钮上,用于放置“提交”,当我选择正确答案a、b、c时,它可以正常工作。但当我选择答案时,a、a、a、click事件处理程序会多次触发 我在网上找到了答案,他们建议使用off(),但它似乎不起作用 这是html <section class="test_your_english_section"> <h3 class="test_you
<section class="test_your_english_section">
<h3 class="test_your_english_section_title">
Test your English
</h3>
<p class="test_your_english_section_desc">
for the questions below, please choose the best option to complete the sentence or conversation
</p>
<span>
Page
<strong id="question_completed_count"> 1 </strong>
of <span>3</span>
</span>
<div class="progressBar" data-page-number="1" data-page-count="5">
<div class="innerBar" style="width: 0%;"></div>
</div>
<form id="form_app">
<fieldset >
<legend>
<div class="clearfix">
<strong>1</strong>
<span>Can I park here?</span>
</div>
</legend>
<div class="clearfix" id="q_1" onclick="checkClick(this)">
<label>
<span>Sorry,I did that.</span>
<input type="radio" name="q_1" value="a">
</label>
<em></em>
<label>
<span> It's the same place..</span>
<input type="radio" name="q_1" value="b">
</label>
<em></em>
<label>
<span> Only for half an hour.</span>
<input type="radio" name="q_1" value="c">
</label>
<em></em>
</div>
</fieldset>
<fieldset >
<legend>
<div class="clearfix">
<strong>2</strong>
<span>What colour will you paint the children's bedroom?</span>
</div>
</legend>
<div class="clearfix" id="q_2" onclick=checkClick(this)>
<label>
<span>I hope it was right.</span>
<input type="radio" name="q_2" value="a">
</label>
<em></em>
<label>
<span>We can't decide.</span>
<input type="radio" name="q_2" value="b">
</label>
<em></em>
<label>
<span>It wasn't very difficult.</span>
<input type="radio" name="q_2" value="c">
</label>
<em></em>
</div>
</fieldset>
<fieldset >
<legend>
<div class="clearfix">
<strong>3</strong>
<span>What colour will you paint the children's bedroom?</span>
</div>
</legend>
<div class="clearfix" id="q_3" onclick=checkClick(this)>
<label id="q_31">
<span>I hope it was right.</span>
<input type="radio" name="q_3" value="a">
</label>
<em></em>
<label id="q_32">
<span>We can't decide.</span>
<input type="radio" name="q_3" value="b">
</label>
<em></em>
<label id="q_33">
<span>It wasn't very difficult.</span>
<input type="radio" name="q_3" value="c">
</label>
<em></em>
</div>
</fieldset>
</form>
<button id="submit" onclick="showResults()">Submit Quiz</button>
测试你的英语
对于下面的问题,请选择完成句子或对话的最佳选项
页
1
共3人
1
我可以在这里停车吗?
对不起,是我干的。
这是同一个地方。。
只有半个小时。
2
你要把孩子们的卧室漆成什么颜色?
我希望这是对的。
我们不能决定。
这并不难。
3
你要把孩子们的卧室漆成什么颜色?
我希望这是对的。
我们不能决定。
这并不难。
提交测验
以及具有正在运行的单击事件处理程序的函数;希望这很容易理解(如果不是,请告诉我,我也想在这方面做得更好):
innerBar=$('.innerBar')[0];
设checkedCount=0;
const totalquestionscont=3;
让checked1=false;
让checked2=false;
让checked3=false;
功能检查单击(输入分区){
console.log(checkedCount);
开关(输入分区id){
案例“q_1”:
如果(选中1)
打破
checked1=真;
checkedCount++;
打破
案例“q_2”:
如果(选中2)
打破
checked2=真;
checkedCount++;
打破
案例“q_3”:
如果(选中3)
打破
checked3=真;
checkedCount++;
打破
}
progressRate=((checkedCount/TotalQuestionsCount)*100);
innerBar.style.width=progressRate;
}
函数showResults(){
var radio_names=['q_1'、'q_2'、'q_3'];
var correctAnwers=['a','b','c'];
var totalScore=0;
//检查所有答案
如果(checkedCount!==TotalQuestionsCount){
警惕(“请回答所有问题!”);
返回;
}
//重复电台名称并检查相应的答案:
对于(i=0;i尝试这样声明i
:
for (var i=0;i<TotalQuestionsCount;i++) {
....
}
用于(var i=0;i这有什么区别吗?代码看起来不错。我自己测试它时只能确认,如果你声明i
代码会工作得很好,如果没有声明,它将进入无限循环。我想你必须声明每个变量,即使在for
中,你也应该始终声明每个变量:)但是,在不解释为什么有必要的情况下建议进行更改并不重要。OP代码的问题在于,它们有两个for
循环,它们都使用全局i
作为循环变量。因此,当执行getRadioBtn
时,它实际上会更改中for
循环的i
ts也使用。好的,谢谢。这正是我要找的(不知道为什么我以前没有找到它)
for (var i=0;i<TotalQuestionsCount;i++) {
....
}