有点新jQuery和我有一个多答案测验问题。它正常工作,直到我添加自定义错误消息。多自动测验在jquery自定义提醒
该问题有多个正确的答案,检查顶部的变量。我希望这个问题也能检查他们是否选中了所有的复选框或者没有复选框。它通过警报消息向用户提供反馈。
以下是我的jsfiddle ...单击“提交”按钮时不检查任何框,您将看到自定义警报显示两次(第二个警报错误)。然后注释掉替换警告框的第一个“window.alert”行代码,它会显示一个错误,然后显示另一个错误(理想方式)。
有没有更好的方法,我可以构建我的if语句来同时检查盒子检查量和正确答案?
<p>Please select the 5 correct answers:</p>
<label><input type="checkbox" name="q1" class="wrong"></input>Answer 0 </label>
<label><input type="checkbox" name="q1" id="q1-a"></input>Answer 1</label>
<label><input type="checkbox" name="q1" class="wrong"></input>Answer 2 </label>
<label><input type="checkbox" name="q1" id="q1-b"></input>Answer 3</label>
<label><input type="checkbox" name="q1" id="q1-c" ></input>Answer 4 </label>
<label><input type="checkbox" name="q1" id="" class="wrong"></input>Answer 5</label>
<label><input type="checkbox" name="q1" id="" class="wrong"></input>Answer 6</label>
<label><input type="checkbox" name="q1" id="q1-d" ></input>Answer 7</label>
<label><input type="checkbox" name="q1" id="q1-e" ></input>Answer 8</label>
<br />
<button class="submit1">Submit1</button>
<div id="messageBox"> </div>
#messageBox {
position: fixed;
top:40%;
left: 20px;
width: 240px;
height:auto;
font-family: Arial, Helvetica, Sans-Serif;
font-size: 12px;
background-color:#F93;
color: #FFFFFF;
padding: 6px;
display: none;
border: 1px solid #ccc;
border-radius: 4px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
box-shadow: 8px 8px 8px #000;
padding: 1em;
}
window.alert = function(message) {
$('#messageBox').text(message).fadeIn().delay(1000).fadeOut('slow');
//comment out this bit of code and you can see my issue.
}
$(function(){
//correct answers stored here by id
var rules = ['q1-a,q1-b,q1-c,q1-d,q1-e'];
$('.submit1').click(function(e) {
e.preventDefault();
///checks to see how many checkboxes have been clicked
var countchecked1 = $("input[name=q1]:checked").length;
if(countchecked1 == 0)
{
alert("You have not selected any checkboxes.");
}
if(countchecked1 == 9)
{
alert("Cheating.. You can't select all the boxes.");
$('input[name=q1]:checked').removeAttr('checked');
return false;
}
//check correct answers from var above
if($('input[name=q1]:checked').map(function(i,v) { return v.id; }).get().join(',') == rules[0]) {
alert("Correct! you selected the correct answers. ");
return false;
}
else
{
$('input[type="checkbox"].wrong:checked').parent('label').addClass('highlight');
$('.wrong:checked').attr('disabled', 'disabled');
$('.wrong:checked').removeAttr('checked');
alert("Incorrect... Please try again");
return false;
}
});
});
解决2警告框是'if(countchecked1 == 0){alert(“您没有选择任何复选框。”); return false;} 我仍在阅读代码以找出可能更好的方法建构如果 – 2013-04-23 17:14:19