简体   繁体   English

如何从文本框中获取文本并进行比较?

[英]How to get the text from the text box and compare it?

I'm doing a website to learn Italian and my problem is when I try to get the answers from a text-box and do the percentage, it always gives me 0% even if i answered them right!我正在做一个学习意大利语的网站,我的问题是当我尝试从文本框中获取答案并计算百分比时,即使我回答正确,它也总是给我 0%!

 var ri = [ 0, 1, 2, 3, 4]; var res=0; var rest; function input(){ ri[0] = document.getElementsByName("r1").value; ri[1] = document.getElementsByName("r2").value; ri[2] = document.getElementsByName("r3").value; ri[3] = document.getElementsByName("r4").value; ri[4] = document.getElementsByName("r5").value; checkAnswers(); } function input(){ if(ri[0]=="abitiamo"){ res=res+1; } if(ri[1]=="frequentate"){ res=res+1; } if(ri[2]=="suonano"){ res=res+1; } if(ri[3]=="trovi"){ res=res+1; } if(ri[4]=="studia"){ res=res+1; } output(); } function output(){ rest = res*100/ri.length; alert(rest+"%"); res=0; rest=0; }
 <p style="font-size: large;"> 1. Noi <input type="text" name="r1" style="width: 86px;"> (abitare) a Roma <br> 2. Che scuola <input type="text" name="r2" style="width: 86px;"> (frequentare) voi, Gianni e Tommaso?<br> 3. Marina e Lucia <input type="text" name="r3" style="width: 86px;"> (suonare) bene il piano. <br> 4. Luca, tu <input type="text" name="r4" style="width: 86px;"> (trovare) delle informazioni interessanti in questa lezione?<br> 5. Luciana <input type="text" name="r5" style="width: 86px;"> (studiare) filosofia a Milano. <br> </p> <button onclick="input()">Click me</button>

This is my solution:这是我的解决方案:

 var answers = ["abitiamo", "frequentate", "suonano", "trovi", "studia"]; var correctCount = 0; var result; function input() { correctCount = 0; result=0; for (i = 0; i < answers.length; i++) { var textBox = document.getElementsByName("r" + (i + 1))[0]; if (textBox.value == answers[i]) { correctCount++; } } result = correctCount * 100 / answers.length; alert(result + "%"); }
 <p style="font-size: large;"> 1. Noi <input type="text" name="r1" style="width: 86px;"> (abitare) a Roma <br> 2. Che scuola <input type="text" name="r2" style="width: 86px;"> (frequentare) voi, Gianni e Tommaso?<br> 3. Marina e Lucia <input type="text" name="r3" style="width: 86px;"> (suonare) bene il piano. <br> 4. Luca, tu <input type="text" name="r4" style="width: 86px;"> (trovare) delle informazioni interessanti in questa lezione?<br> 5. Luciana <input type="text" name="r5" style="width: 86px;"> (studiare) filosofia a Milano. <br> </p> <button onclick="input()">Click me</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM