在jQuery中隐藏以前的html部分
Hide previous html section within jQuery
下面给出的代码用于检查所提供的答案是否正确,可以使用单选按钮选择答案。当下一个按钮被点击,然后第一个问题必须隐藏,下一个问题应显示。我需要另一个按钮"previous",它应该显示以前的问题,如果它被点击,当前的问题必须隐藏。
<div id="question_2" class='questions'>
<h2 id="question_2">2.Questions</h2>
<div class='align'>
<input type="radio" value="1" id='radio1_2' name='2'>
<label id='ans1_2' for='1'>Answer 1</label>
<br/>
<input type="radio" value="2" id='radio2_2' name='2'>
<label id='ans2_2' for='1'>Answer 2</label>
<br/>
<input type="radio" value="3" id='radio3_2' name='2'>
<label id='ans3_2' for='1'>Answer 3</label>
<br/>
<input type="radio" value="4" id='radio4_2' name='2'>
<label id='ans4_2' for='1'>Answer 4</label>
<input type="radio" checked='checked' value="5" style='display:none' id='radio4_2' name='2'>
</div>
<br/>
<input type="button" id='next2' value='Next' name='question' class='button'/>
<input type="button" id="previous" value="Previous" name="question" class="button"/>
</div>
$(document).ready(function(){
$('#demo1').stopwatch().stopwatch('start');
var steps = $('form').find(".questions");
var count = steps.size();
steps.each(function(i){
hider=i+2;
if (i == 0) {
$("#question_" + hider).hide();
createNextButton(i);
}
else if(count==i+1){
var step=i + 1;
//$("#next"+step).attr('type','submit');
$("#next"+step).on('click',function(){
submit();
});
}
else{
$("#question_" + hider).hide();
createNextButton(i);
}
});
function submit(){
$.ajax({
type: "POST",
url: "ajax.php",
data: $('form').serialize(),
success: function(msg) {
$("#quiz_form,#demo1").addClass("hide");
$('#result').show();
$('#result').append(msg);
}
});
}
function createNextButton(i){
var step = i + 1;
var step1 = i + 2;
$('#next'+step).on('click',function(){
$("#question_" + step).hide();
$("#question_" + step1).show();
});
}
setTimeout(function() {
submit();
}, 50000);
});
检查这个:
我使用currentStep输入很容易看到我们在哪个问题上。
如果你喜欢下一个或上一个按钮,你可以检查你是在第一个问题还是在最后一个问题上隐藏或显示下一个和上一个按钮。
当你在最后一个问题,你点击下一个,它会提出提交。我把它改成了一个提醒来查看它。
https://jsfiddle.net/netvicious/7hzgdzcp/<input type='text' id='currentStep' value='1'>
<div id="question_1" class='questions'>
<h2>Question 1</h2>
<div class='align'>
<input type="radio" value="1" id='radio1_1' name='1' />
<label id='ans1_1' for='1'>Answer 1</label>
<br/>
<input type="radio" value="2" id='radio2_1' name='1' />
<label id='ans2_1' for='1'>Answer 2</label>
<br/>
<input type="radio" value="3" id='radio3_1' name='1' />
<label id='ans3_1' for='1'>Answer 3</label>
<br/>
<input type="radio" value="4" id='radio4_1' name='1' />
<label id='ans4_1' for='1'>Answer 4</label>
</div>
</div>
<div id="question_2" class='questions'>
<h2>Question 2</h2>
<div class='align'>
<input type="radio" value="1" id='radio1_2' name='2' />
<label id='ans1_2' for='2'>Answer 1</label>
<br/>
<input type="radio" value="2" id='radio2_2' name='2' />
<label id='ans2_2' for='2'>Answer 2</label>
<br/>
<input type="radio" value="3" id='radio3_2' name='2' />
<label id='ans3_2' for='2'>Answer 3</label>
<br/>
<input type="radio" value="4" id='radio4_2' name='2' />
<label id='ans4_2' for='2'>Answer 4</label>
</div>
</div>
<br/>
<input type="button" id='next' value='Next' name='question' class='button' />
<input type="button" id="previous" value="Previous" name="question" class="button" />
</div>
$(document).ready(function() {
steps = $('.questions').size();
$('.questions').hide();
$("#question_1").show();
$('#next').on('click', function() {
$('.questions').hide();
elem = parseInt(parseInt($('#currentStep').val()) + 1);
if (elem == steps + 1) {
submit(); // If the current one was the last submit
} else {
$("#question_" + elem).show();
$('#currentStep').val(elem);
}
});
$('#previous').on('click', function() {
elem = parseInt(parseInt($('#currentStep').val()) - 1);
if (elem == 0) return; // It was the first question so no previous one
$('.questions').hide();
$("#question_" + elem).show();
$('#currentStep').val(elem);
});
function submit() {
alert('posting');
exit;
$.ajax({
type: "POST",
url: "ajax.php",
data: $('form').serialize(),
success: function(msg) {
$("#quiz_form,#demo1").addClass("hide");
$('#result').show();
$('#result').append(msg);
}
});
}
});
相关文章:
- 突出显示与perl正则表达式匹配的HTML部分 - 在服务器端perl或客户端javascript中执行此操作
- 无法在android网络视图中使用AJAX加载HTML部分
- 使用 Javascript 解析电子邮件源的文本/html 部分
- 如何在另一个 html 页面中呈现 html 部分
- 如何在 angularjs 中绑定为字符串的 html 部分
- 我可以使用/包含 HTML 部分,客户端,使用 EJS(或类似)
- Gulp.js HTML 部分和循环
- 将 HTML 部分替换为 replace()
- 将不透明度层添加到带有侧边栏的HTML部分.js / jQuery
- 我怎样才能获得剥离后产生某个字符串的 html 部分
- 如何使用html部分中声明的变量
- 如何链接HTML部分
- 如何使用jQueryappend插入HTML部分包含onclick函数,该函数有一个参数
- 使用knockoutjs虚拟元素动态创建html部分
- 显示链接上的html部分活动/点击-使用Rails4引导
- javascript函数在单击时展开html部分
- c#谷歌地图API - HTML部分的javascript错误
- 管理离线web应用程序的html部分
- 用angularjs隐藏HTML部分,点击右键
- 通过悬停来隐藏/取消隐藏HTML部分