单选按钮 - 使用 jQuery 显示基于选中按钮的 DIV

发布于 2024-09-12 02:40:00 字数 1373 浏览 4 评论 0原文

我正在尝试实现以下目标 - 从每个都有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱人配狗天长地久 2024-09-19 02:40:00

如果你想让它更通用一点,你可以根据值来做,如下所示:

$("#question_one :radio[name=question_1]").change(function() {
    $("div.show_answer").hide();
    $("#q1_" + $(this).val()).show(100);
});​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

你可以在这里尝试一下,或者如果您需要更通用的方法,假设您有多个问题,只需将上面的每个 HTML 块包装在一个容器中,例如

那么你可以这样做:

$(".question :radio[name=question_1]").change(function() {
    $(this).closest(".question").find("div.show_answer").hide()
           .filter("[id$='" + $(this).val() +"']").show(100);
});​

你可以测试一下此处的版本,它将适用于多个问题,而无需为每个问题重复代码。


评论:要在第一次单击后杀死它,您有几个选项,您可以通过将其添加到处理程序来禁用它们(在这里测试):

$(this).closest('li').find(':radio').attr('disabled', true);

或者如果您想启用更改但不再显示答案,只需取消绑定它们即可 (在此处测试):

$(this).closest('li').find(':radio').unbind('change');

If you want it to be a bit more generic, you can do it based on the value, like this:

$("#question_one :radio[name=question_1]").change(function() {
    $("div.show_answer").hide();
    $("#q1_" + $(this).val()).show(100);
});​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

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:

$(".question :radio[name=question_1]").change(function() {
    $(this).closest(".question").find("div.show_answer").hide()
           .filter("[id$='" + $(this).val() +"']").show(100);
});​

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):

$(this).closest('li').find(':radio').attr('disabled', true);

Or if you want to enable change but not show the answer again, just unbind them (test here):

$(this).closest('li').find(':radio').unbind('change');
最偏执的依靠 2024-09-19 02:40:00

尝试使用伪选择器 :checked,如下所示:
$(“#question_one 输入:已检查”)....

Try using the pseudo-selector :checked, like this:
$("#question_one input:checked")....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文