单选按钮 - 使用 jQuery 显示基于选中按钮的 DIV
我正在尝试实现以下目标 - 从每个都有 4 个多项选择答案的问题列表中,我需要通过按下单选按钮后显示答案是正确还是不正确来显示用户是否(立即)回答正确。
这是一个问题的示例:
<ol>
<li id="question_one">
<p><input type="radio" name="question_1" value="answer_1" />Corn</p>
<p><input type="radio" name="question_1" value="answer_2" />Cotton</p>
<p><input type="radio" name="question_1" value="answer_3" />Rice</p>
<p><input type="radio" name="question_1" value="answer_4" />Soybeans</p>
</li>
</ol>
<div id="q1_answer_1" class="show_answer">Corn 11.0%</div>
<div id="q1_answer_2" class="show_answer">Cotton 2.5%</div>
<div id="q1_answer_3" class="show_answer">Rice 11.0%</div>
<div id="q1_answer_4" class="show_answer">Soybeans 7.0%</div>
生成的 jquery:
$("#question_one input:radio:eq(answer_1)").click(function(){
$("#q1_answer_1").show(100);
});
$("#question_one input:radio:eq(answer_2)").click(function(){
$("#q1_answer_2").show(100);
});
$("#question_one input:radio:eq(answer_3)").click(function(){
$("#q1_answer_3").show(100);
});
$("#question_one input:radio:eq(answer_4)").click(function(){
$("#q1_answer_4").show(100);
});
我相信这不起作用,因为 eq()
仅适用于数值?是否有等效的 eq()
适用于输入值?或者关于上述如何工作的任何其他建议?
非常感谢
I am trying to achieve the following - from a list of questions each with 4 multiple choice answers I need to show whether the user has answered correctly (immediately) by showing whether it was correct or incorrect once a radio button has been pressed.
Here is an example of a question:
<ol>
<li id="question_one">
<p><input type="radio" name="question_1" value="answer_1" />Corn</p>
<p><input type="radio" name="question_1" value="answer_2" />Cotton</p>
<p><input type="radio" name="question_1" value="answer_3" />Rice</p>
<p><input type="radio" name="question_1" value="answer_4" />Soybeans</p>
</li>
</ol>
<div id="q1_answer_1" class="show_answer">Corn 11.0%</div>
<div id="q1_answer_2" class="show_answer">Cotton 2.5%</div>
<div id="q1_answer_3" class="show_answer">Rice 11.0%</div>
<div id="q1_answer_4" class="show_answer">Soybeans 7.0%</div>
And the resulting jquery:
$("#question_one input:radio:eq(answer_1)").click(function(){
$("#q1_answer_1").show(100);
});
$("#question_one input:radio:eq(answer_2)").click(function(){
$("#q1_answer_2").show(100);
});
$("#question_one input:radio:eq(answer_3)").click(function(){
$("#q1_answer_3").show(100);
});
$("#question_one input:radio:eq(answer_4)").click(function(){
$("#q1_answer_4").show(100);
});
I believe this won't work because eq()
will only work for numeric values? Is there an equivalent of eq()
that will work for input values? Or any other suggestions as to how the above might work?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想让它更通用一点,你可以根据值来做,如下所示:
你可以在这里尝试一下,或者如果您需要更通用的方法,假设您有多个问题,只需将上面的每个 HTML 块包装在一个容器中,例如
那么你可以这样做:
你可以测试一下此处的版本,它将适用于多个问题,而无需为每个问题重复代码。
评论:要在第一次单击后杀死它,您有几个选项,您可以通过将其添加到处理程序来禁用它们(在这里测试):
或者如果您想启用更改但不再显示答案,只需取消绑定它们即可 (在此处测试):
If you want it to be a bit more generic, you can do it based on the value, like this:
You can give it a try here, or if you need it more generic, say you have multiple questions, just wrap each of the blocks of HTML above in a container, for example
<div class="question"></div>
then you can do something like this:You can test that version here, it'll work for multiple questions without duplicating your code for each.
For comments: To kill it after the first click you have a few options, you could disable them by adding this to the handler (test here):
Or if you want to enable change but not show the answer again, just unbind them (test here):
尝试使用伪选择器 :checked,如下所示:
$(“#question_one 输入:已检查”)....
Try using the pseudo-selector :checked, like this:
$("#question_one input:checked")....