我正在为一家用户回答三个问题的公司进行测验,如果他们都是正确的,他们应该被重定向到一个页面,在那里他们填写他们的联系信息,以便有机会赢得奖品。 如果一个或多个问题的回答错误,则应将其重定向到另一个页面。
我需要标记正确的答案,然后检查它们。 如果所有问题都得到了正确回答:重定向到motivering.html 如果一个或多个问题出错:重定向到tyvarr.html
我试图检查这些值,然后才想出来。这不正确,我几乎可以肯定它不是最合适的方式。
的jQuery
$(document).ready(function() {
$('#questions input').on('change', function() {
console.log($('input[name=questionOne]:checked', '#questions').val());
console.log($('input[name=questionTwo]:checked', '#questions').val());
console.log($('input[name=questionThree]:checked', '#questions').val());
});
});
我的标记 - HTML
<form method="POST" id="questions">
<div class="form-group" id="questionOne">
<h3>Question 1</h3>
<div class="radio">
<input type="radio" name="questionOne" id="question1" class="radio" value="1" required/>
<label for="question1">Option 1</label>
</div>
<div class="radio">
<input type="radio" name="questionOne" id="question2" class="radio" value="2"/>
<label for="question2">Option 2 - Right answer</label>
</div>
<div class="radio">
<input type="radio" name="questionOne" id="question3" class="radio" value="3"/>
<label for="question3">Option 3</label>
</div>
</div>
<div class="form-group" id="questionTwo">
<h3>Question 2</h3>
<div class="radio">
<input type="radio" name="questionTwo" id="question4" class="radio" value="4" required/>
<label for="question4">Option 1</label>
</div>
<div class="radio">
<input type="radio" name="questionTwo" id="question5" class="radio" value="5"/>
<label for="question5">Option 2</label>
</div>
<div class="radio">
<input type="radio" name="questionTwo" id="question6" class="radio" value="6"/>
<label for="question6">Option 3 - Right answer</label>
</div>
</div>
<div class="form-group" id="questionThree">
<h3>Question 3</h3>
<div class="radio">
<input type="radio" name="questionThree" id="question7" class="radio" value="7" required/>
<label for="question7">Option 1</label>
</div>
<div class="radio">
<input type="radio" name="questionThree" id="question8" class="radio" value="8"/>
<label for="question8">Option 2 - Right answer</label>
</div>
<div class="radio">
<input type="radio" name="questionThree" id="question9" class="radio" value="9"/>
<label for="question9">Option 3</label>
</div>
</div>
<div class="buttons">
<a href="index.html" class="btn btn-default form_button_cancel">Avbryt</a>
<button type="submit" class="btn btn-default form_button_next">Nästa</button>
</div>
</form>
我的风格 - CSS
@charset "utf-8";
/* CSS Document */
/* FONT */
@import url('https://fonts.googleapis.com/css?family=Nunito:400,700');
body, textarea, input{
font-family: 'Nunito', sans-serif;
font-weight:400;
font-size:16px;
line-height:26px;
letter-spacing:0.5px;
}
/*.start{
background-color:tomato;
}*/
h1, h2, h3, .form_button_start, .form_button_cancel, .form_button_next{
font-family: 'Nunito', sans-serif;
font-weight:600;
}
h1, h2, h3{
text-transform:uppercase;
}
h3{
font-size:21px;
margin-top:50px;
}
textarea{
resize:none;
}
ul{
list-style:none;
}
.radio label {
width: 100%;
border-radius: 3px;
border: 1px solid #D1D3D4
}
/* hide input */
input.radio:empty {
margin-left: -999px;
}
/* style label */
input.radio:empty ~ label {
position: relative;
line-height: 3.5em;
text-indent: 2em;
margin-top: 1em;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
font-size:16px;
text-transform:uppercase;
}
input.radio:empty ~ label:before {
position: absolute;
display: block;
top: 0;
bottom: 0;
left: 0;
content: '';
width: 2.5em;
background: #D1D3D4;
border-radius: 3px 0 0 3px;
}
/* toggle hover */
input.radio:hover:not(:checked) ~ label:before {
/*content:'\2714';*/
text-indent: .9em;
color: #C2C2C2;
}
input.radio:hover:not(:checked) ~ label {
color: #888;
}
/* toggle on */
input.radio:checked ~ label:before {
/*content:'\2714';*/
text-indent: .9em;
color: #9CE2AE;
background-color: #4DCB6D;
}
input.radio:checked ~ label {
color: #333;
}
/* radio focus */
input.radio:focus ~ label:before {
box-shadow: 0 0 0 3px #999;
}
/* Butto style */
.form_button_cancel, .form_button_next, .form_button_start{
width: 200px;
text-transform: uppercase;
line-height: 30px;
margin: 20px 0;
}
.form_button_start{
background-color:green;
border:0px solid transparent;
}
.form_button_next{
background-color: green;
float:right;
border:0px solid transparent;
}
.form_button_cancel{
float:left;
}
答案 0 :(得分:1)
$(document).ready(function() {
$('#questions input').on('change', function() {
/* Answers array, if there are more questiones I would think about changing questionOne to question1
* and build this array in a loop getting values by something like:
* userAnswers.push($('input[name=question'+ i +']:checked', '#questions).val());
* or even group them under one class and query for .question:checked
*/
var userAnswers = [
$('input[name=questionOne]:checked', '#questions').val(),
$('input[name=questionTwo]:checked', '#questions').val(),
$('input[name=questionThree]:checked', '#questions').val()
];
//Array that contains right answers, indexes must match userAnswers array.
var rightAnswers = [2, 6, 8];
//Check if all answers are defined
for(var i = 0; i < userAnswers.length; i++){
if(typeof userAnswers[i] === 'undefined'){
return;
}
}
//Check if all answers are rightAnswers
for(var i = 0; i < userAnswers.length; i++){
if(userAnswers[i] != rightAnswers[i]){
return console.log('Found wrong answer!');
}
}
console.log('All answers are correct.');
});
});
&#13;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="index.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<form method="POST" id="questions">
<div class="form-group" id="questionOne">
<h3>Question 1</h3>
<div class="radio">
<input type="radio" name="questionOne" id="question1" class="radio" value="1" required/>
<label for="question1">Option 1</label>
</div>
<div class="radio">
<input type="radio" name="questionOne" id="question2" class="radio" value="2"/>
<label for="question2">Option 2 - Right answer</label>
</div>
<div class="radio">
<input type="radio" name="questionOne" id="question3" class="radio" value="3"/>
<label for="question3">Option 3</label>
</div>
</div>
<div class="form-group" id="questionTwo">
<h3>Question 2</h3>
<div class="radio">
<input type="radio" name="questionTwo" id="question4" class="radio" value="4" required/>
<label for="question4">Option 1</label>
</div>
<div class="radio">
<input type="radio" name="questionTwo" id="question5" class="radio" value="5"/>
<label for="question5">Option 2</label>
</div>
<div class="radio">
<input type="radio" name="questionTwo" id="question6" class="radio" value="6"/>
<label for="question6">Option 3 - Right answer</label>
</div>
</div>
<div class="form-group" id="questionThree">
<h3>Question 3</h3>
<div class="radio">
<input type="radio" name="questionThree" id="question7" class="radio" value="7" required/>
<label for="question7">Option 1</label>
</div>
<div class="radio">
<input type="radio" name="questionThree" id="question8" class="radio" value="8"/>
<label for="question8">Option 2 - Right answer</label>
</div>
<div class="radio">
<input type="radio" name="questionThree" id="question9" class="radio" value="9"/>
<label for="question9">Option 3</label>
</div>
</div>
<div class="buttons">
<a href="index.html" class="btn btn-default form_button_cancel">Avbryt</a>
<button type="submit" class="btn btn-default form_button_next">Nästa</button>
</div>
</form>
</body>
</html>
&#13;
对于重定向而不是打印&#39;发现错误答案!&#39;或者&#39;所有答案都是正确的!&#39;在控制台中,你会这样做:
//Check if all answers are rightAnswers
for(var i = 0; i < userAnswers.length; i++){
if(userAnswers[i] != rightAnswers[i]){
window.location.href = 'wrong.html';
return; // End here, and do not execute further redirection.
}
}
window.location.href = 'right.html';
return
内部循环确保脚本将在那里结束函数执行,并且它不会执行&#39; right.html&#39;重定向。
这两个循环,用于检查答案是否已定义,以及它们是否正确可以合并为一个,但是还有一些代码会注意到所有答案都已定义。
编辑:要在下面的评论中回答您的问题,首先我想我会将其绑定到按钮onclick
事件,但要保持验证在这种情况下很方便(当一些时候如果问题没有得到回答,请注意用户注意事项。我认为表格onsubmit
会更好。我仍然会阻止默认行为,因此表单并未真正提交,但请记住,如果有人将js禁用,它将提交表单以便牢记这一点。因此,下一个按钮是提交类型,它将尝试提交表单,在onsubmit
事件处理程序中我阻止表单提交并检查答案。
$(document).ready(function() {
$('#questions').on('submit', function(e) {
e.preventDefault(); // Prevent form submition;
var userAnswers = [
$('input[name=questionOne]:checked', '#questions').val(),
$('input[name=questionTwo]:checked', '#questions').val(),
$('input[name=questionThree]:checked', '#questions').val()
];
//Array that contains right answers, indexes must match userAnswers array.
var rightAnswers = [2, 6, 8];
//Check if all answers are defined
for(var i = 0; i < userAnswers.length; i++){
if(typeof userAnswers[i] === 'undefined'){
return;
}
}
//Check if all answers are rightAnswers
for(var i = 0; i < userAnswers.length; i++){
if(userAnswers[i] != rightAnswers[i]){
return console.log('Found wrong answer!');
}
}
console.log('All answers are correct.');
});
});
&#13;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="index.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<form method="POST" id="questions">
<div class="form-group" id="questionOne">
<h3>Question 1</h3>
<div class="radio">
<input type="radio" name="questionOne" id="question1" class="radio" value="1" required/>
<label for="question1">Option 1</label>
</div>
<div class="radio">
<input type="radio" name="questionOne" id="question2" class="radio" value="2"/>
<label for="question2">Option 2 - Right answer</label>
</div>
<div class="radio">
<input type="radio" name="questionOne" id="question3" class="radio" value="3"/>
<label for="question3">Option 3</label>
</div>
</div>
<div class="form-group" id="questionTwo">
<h3>Question 2</h3>
<div class="radio">
<input type="radio" name="questionTwo" id="question4" class="radio" value="4" required/>
<label for="question4">Option 1</label>
</div>
<div class="radio">
<input type="radio" name="questionTwo" id="question5" class="radio" value="5"/>
<label for="question5">Option 2</label>
</div>
<div class="radio">
<input type="radio" name="questionTwo" id="question6" class="radio" value="6"/>
<label for="question6">Option 3 - Right answer</label>
</div>
</div>
<div class="form-group" id="questionThree">
<h3>Question 3</h3>
<div class="radio">
<input type="radio" name="questionThree" id="question7" class="radio" value="7" required/>
<label for="question7">Option 1</label>
</div>
<div class="radio">
<input type="radio" name="questionThree" id="question8" class="radio" value="8"/>
<label for="question8">Option 2 - Right answer</label>
</div>
<div class="radio">
<input type="radio" name="questionThree" id="question9" class="radio" value="9"/>
<label for="question9">Option 3</label>
</div>
</div>
<div class="buttons">
<a href="index.html" class="btn btn-default form_button_cancel">Avbryt</a>
<button type="submit" class="btn btn-default form_button_next">Nästa</button>
</div>
</form>
</body>
</html>
&#13;