我正在尝试根据单选按钮选择显示特定的跨度消息。另外,我想在用户做出选择后立即锁定选择。换句话说:
我几乎可以实现我想要创建的效果: http://jsfiddle.net/baumdexterous/HkFXs/1/
但我不确定如何在选择错误选项时显示错误的部分。我仍然是一名初学程序员,不知道如何使它工作。请参阅下面的JS和HTML。希望了解如何使这项工作!
HTML
<div id="wizard">
<div id="space" style="width:810px; background:white; height:10px;">
</div>
<div class="items">
<!-- Question 1 -->
<div class="page one">
<h2><strong>Question 1:</strong></h2>
<ul>
<li class="required double">
<label>
<div class="qselections required">
<input type="radio" value="a" name="question1">a) New York
<div id="questiononea"></div>
<br>
<input type="radio" value="b" name="question1">b) Washington DC
<br>
<input type="radio" value="c" name="question1">c) Seattle
<br>
<input type="radio" value="d" name="question1">d) Portland
<br>
</div>
</label>
</li>
</ul>
<li class="clearfix">
<button type="button" class="next right">Proceed »</button>
</li>
</div>
</div>
</div><!--items-->
</div><!--wizard-->
</form>
的JavaScript
$(function () {
var root = $("#wizard").scrollable();
var isRadioCheck = false;
// some variables that we need
var api = root.scrollable(),
drawer = $("#drawer");
// prevent the user from making another selection once one radio option has been selected
$('input[type=radio]').click(function(){ $('input[type=radio]').prop('disabled',true); });
// Show user correct answer
var appended = " <span id='qselectionsCORRECT' style='font-size:16px; line-height:16px;'>Correct</span>";
appended.id = 'appended';
$('input:radio[name="question1"]').change(
function(){
if ($(this).val() == 'a') {
$(appended).appendTo('#questiononea');
}
else {
$(appended).remove();
}
});
// validation logic is done inside the onBeforeSeek callback
api.onBeforeSeek(function (event, i) {
// we are going 1 step backwards so no need for validation
if (api.getIndex() < i || $('input[type=radio]').is(':checked')) {
// 1. get current page
var page = root.find(".page,.qselections").eq(api.getIndex()),
// 2. .. and all required fields inside the page
inputs = page.find(".required :input").removeClass("error"),
// 3. .. which are empty
empty = inputs.filter(function () {
isRadioCheck = $('input[type=radio]').is(':checked');
return $(this).val().replace(/\s*/g, '') == '';
});
//ealert('Empty Value is bool : ' + empty.length + isRadioCheck);
if (isRadioCheck) {
$('.qselections').removeClass("error");
//alert('removed');
}
// if there are empty fields, then
if (empty.length || !isRadioCheck) {
// slide down the drawer
drawer.slideDown(function () {
// colored flash effect
drawer.css("backgroundColor", "#fff");
setTimeout(function () {
drawer.css("backgroundColor", "#fff");
}, 1000);
});
// add a CSS class name "error" for empty & required fields
empty.addClass("error");
if (!isRadioCheck) $('.qselections').addClass("error");
// cancel seeking of the scrollable by returning false
return false;
// everything is good
} else {
// hide the drawer
drawer.slideUp();
}
}
// update status bar
$("#status li").removeClass("active").eq(i).addClass("active");
});
// if tab is pressed on the next button seek to next page
root.find("button.next").keydown(function (e) {
if (e.keyCode == 9) {
// seeks to next tab by executing our validation routine
api.next();
e.preventDefault();
}
});
});
但是
答案 0 :(得分:1)
为什么不用CSS修复它?使用javascript只需在输入中添加一个true / false(或其他)类,如下所示:
<input type="radio" value="a" class="true" />
<input type="radio" value="b" class="false" />
使用CSS,使用:after-pseudo
input.true:after {
content:'Correct';
color:green;
}
input.false:after {
content:'Incorrect';
color:red;
}