javascript - javascript中的测验,隐藏一个并在jquery中显示另一个问题
问题描述
我正在尝试在 jquery 中进行测验,我需要隐藏问题并显示下一个问题。最后提交表格。
问题是,当我按下下一步而不是一一按下时,它会显示所有问题。
任何想法如何解决它?
html
<form>
<div class="hideShow">
question a
<input type="radio" name="a" value="l"> answer a1
<input type="radio" name="a" value="d"> answer a2
<input type="button" class="next" value="Next">
</div>
<div class="hideShow">
question b
<input type="radio" name="b" value="l"> answer b1
<input type="radio" name="b" value="d"> answer b2
<input type="button" value="Preview">
<input type="button" value="Next">
</div>
<div class="hideShow">
question c
<input type="radio" name="c" value="l"> answer c1
<input type="radio" name="c" value="d"> answer c2
<input type="button" value="Preview">
<input type="button" value="Next">
</div>
<div class="hideShow">
question d
<input type="radio" name="d" value="l"> answer d1
<input type="radio" name="d" value="d"> answer d2
<input type="button" value="Preview">
<input type="button" value="Next">
</div>
<div class="hideShow">
question e
<input type="radio" name="e" value="l"> answer e1
<input type="radio" name="e" value="d"> answer e2
<input type="button" value="Preview">
<input type="submit" value="Show result">
</div>
</form>
jQuery
$(function() {
$(".hideShow").first().css("display", "block");
$( document ).on( "click", ".next", function() {
$(".hideShow").next().toggle();
});
});
CSS:
.hideShow{
display:none;
border:1px solid #000;
padding:10px;
width:100%;
margin-bottom:10px;
}
https://jsfiddle.net/adzh6t5e/
谢谢
解决方案
你可以试试这个:
$( document ).on( "click", ".next", function() {
$(this).closest('.hideShow').hide().next().show();
});
此外,在您的 html 中,只有第一个Next
按钮具有 class .next
。在测试之前将相同的类添加到所有下一个按钮。
推荐阅读
- javascript - 连接代码运行非常缓慢的谷歌脚本
- javascript - 排除站点锚链接 JavaScript
- python-c-api - Python C API - 如何从你自己的 python 类继承?
- azure-devops - 触发 Azure DevOps 发布定义的多个主要项目
- javascript - 编辑现有的 React D3 强制布局图以使用圆形而不是提供的图像
- swift - 在 Swift 中对闭包的困惑强烈地捕获了其中使用的变量
- sql - 仅计算 Oracle (Toad) 中的 MaxDate 记录
- python - Python中的离散颜色条
- javascript - 出现错误 React 无法识别 DOM 元素上的 `handleChange` 道具
- arrays - 将对象推入数组返回一个空数组