按Enter键如何在测验中得到正确的分数? [英] How to get correct score in quiz by pressing enter ?
本文介绍了按Enter键如何在测验中得到正确的分数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开展一个不是多项选择的测验项目。用户每次都必须在输入字段中键入答案。我面临的问题是,如果按下提交按钮,我每次都会得到正确的分数,但如果按下输入提交答案则没有得到正确的分数。
我尝试过:
<!DOCTYPE html>
< html>
< head>
< meta charset =utf-8>
< meta name =viewportcontent =width = device-width,initial-scale = 1>
< meta name =descriptioncontent =每个
的无限语法练习练习以及每个主题,以获得良好的英语语言>
< link rel =stylesheettype =text / csshref =random.css>
< script type =text / javascriptsrc =jquery.js> < /脚本>
< script type =text / javascriptsrc =random.js> < /脚本>
< title>英语语法练习< / title>
< / head>
< title>语法测验< / title>
< body>
< div class =start>
< h1>欢迎来到英语学生< / h1>
< a class =btnhref =#>开始使用< / a>
< / div>
< div class =quiz>
< input type =textclass =input/>
< input type =textclass =output/>
< a class =subhref =#onclick =checkAnswer()>提交< / a>
< / div>
< div class =summary>
< h2>摘要屏幕< / h2>
< p>恭喜你得到了正确的x! < / p为H.
< a class =rsthref =#onclick =restartQuiz()>重新启动测验< / a>
< / div>
< / body>
< / html>
var questions = [
{
title: 我喜欢板球。,
回答: 我不喜欢板球。
},
{
title: 他很聪明。,
回答: 他不聪明。
},
{
title: < span class =code-string>她唱得很好。,
回答: 她唱得不好。
},
{
title: 你是一个ch吃。,
回答: 你不是骗子。
},
{
title: 他们来了。 ,
回答: 他们不会来。
},
];
让得分= 0 ;
让 currentQuestion = 0 ;
$(' document')。ready( function (){
$(' .start a')。click( function (e){
e.preventDefault();
$(' .start')。hide();
$(' 。quiz')。show();
showQuestion();
});
$(' .output')。keyup( function ( e){
if (e.keyCode === 13 ){
$(' 。sub')。click();
}
} );
});
function showQuestion(){
让 question = questions [currentQuestion];
$(' .input')。val(question.title);
}
function checkAnswer(){
让 question = questions [currentQuestion];
让 out = $(' 。output 跨度>)VAL();
if (out == questions [currentQuestion] .answer){
score ++;
}
currentQuestion ++;
if (currentQuestion> = questions.length){
showSummary();
} else {
showQuestion();
}
$(' 。sub')。点击( function (){
$(' .output')。val(' ');
});
}
function showSummary(){
$(' .quiz')。hide();
$(' 。summary')。show();
$(' 。summary p')。text( 恭喜你得分 +得分+ out of +
questions.length + 正确!跨度>);
}
function restartQuiz(){
$(' 。summary a')。click( function (e){
e.preventDefault();
$(' 。summary')。hide( );
$(' .quiz')。show();
currentQuestion = 0 ;
showQuestion();
});
}
解决方案
(' document')。ready( function (){
(' 。启动')。点击( function (e) {
e.preventDefault();
(' .start 跨度>)隐藏();
I am working on a quiz project which is not multiple choice one. The user has to type the answer in the input field each time. The problem I am facing is that I am getting correct score if clicked on submit button each time but not getting correct score if pressed enter to submit the answer.
What I have tried:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content="Unlimited grammar practice exercises of each & every topic to get a good commmand of English language"> <link rel="stylesheet" type="text/css" href="random.css"> <script type="text/javascript" src="jquery.js"> </script> <script type="text/javascript" src="random.js"> </script> <title> English Grammar Exercises </title> </head> <title> Grammar Quiz </title> <body> <div class="start"> <h1> Welcome to English Pupils </h1> <a class="btn" href="#"> Get Started </a> </div> <div class="quiz"> <input type="text" class="input" /> <input type="text" class="output" /> <a class="sub" href="#" onclick="checkAnswer()"> Submit </a> </div> <div class="summary"> <h2> Summary Screen </h2> <p> Congrats you scored x out of y correct ! </p> <a class="rst" href="#" onclick="restartQuiz()"> Restart Quiz </a> </div> </body> </html>
var questions = [
{
title: "I like cricket.",
answer: "I do not like cricket."
},
{
title: "He is smart.",
answer: "He is not smart."
},
{
title: "She sings well.",
answer: "She does not sing well."
},
{
title: "You are a cheat.",
answer: "You are not a cheat."
},
{
title: "They are coming.",
answer: "They are not coming."
},
];
let score = 0;
let currentQuestion = 0;
$('document').ready(function () {
$('.start a').click(function (e) {
e.preventDefault();
$('.start').hide();
$('.quiz').show();
showQuestion();
});
$('.output').keyup(function (e) {
if (e.keyCode === 13) {
$('.sub').click();
}
});
});
function showQuestion() {
let question = questions[currentQuestion];
$('.input').val(question.title);
}
function checkAnswer() {
let question = questions[currentQuestion];
let out = $('.output').val();
if (out == questions[currentQuestion].answer) {
score++;
}
currentQuestion++;
if (currentQuestion >= questions.length) {
showSummary();
} else {
showQuestion();
}
$('.sub').click(function () {
$('.output').val('');
});
}
function showSummary() {
$('.quiz').hide();
$('.summary').show();
$('.summary p').text("Congrats you scored " + score + " out of " +
questions.length + " correct !");
}
function restartQuiz() {
$('.summary a').click(function (e) {
e.preventDefault();
$('.summary').hide();
$('.quiz').show();
currentQuestion = 0;
showQuestion();
});
}
解决方案
('document').ready(function () {
('.start a').click(function (e) { e.preventDefault();
('.start').hide();
这篇关于按Enter键如何在测验中得到正确的分数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文