【问题标题】:radio button selects form which contains radio button which selects form which contains radio button单选按钮选择包含单选按钮的表单,该单选按钮选择包含单选按钮的表单
【发布时间】:2014-08-20 03:23:36
【问题描述】:

开始返回单选按钮是和否,问题 1,问题 1 是返回 Questio1.1 并隐藏按钮,依此类推。

“是”按钮的作用就像一个魅力,但我不知道如何使“否”按钮也能正常工作

谢谢。

    <div><input type='radio' name='opt' value='1' />I agree to start</div>

    <!-- Form 1-->

    <div class='content' id='content1'>
    <p>This is Question 1
    <br>'yes' will return Question 1.1y
    <br>'no' will return Question 1.1n</p>
    </div>
    <div class='info' id='info1'>
    <div><input type='radio' name='opt' value='2' />yes</div>
    <div><input type='radio' name='opt' value='6' />no</div>
    </div>

    <!-- Form 1.1y-->

    <div class='content' id='content2'>
    <p>This is Question 1.1y
    <br>'yes' will return Question 1.1.1y
    <br>'no' will return Question 1.1.1n</p>
    </div>
    <div class='info' id='info2'>
    <div><input type='radio' name='opt' value='3' />yes</div>
    <div><input type='radio' name='opt' value='7' />no</div>
    </div>

    <!-- Form 1.1.1y-->

    <div class='content' id='content3'>
    <p>This is Question 1.1.1y
    <br>'yes' will return answer_y
    <br>your 'no' will answer_n</p>
    </div>
    <div class='info' id='info3'>
    <div><input type='radio' name='opt' value='4' />yes</div>
    <div><input type='radio' name='opt' value='11' />no</div>
    </div>

    <!-- answer_y-->

    <div class='content' id='content4'>
    <div><p>answer_y</p></div>
    </div>

    <!-- answer_n-->

    <div class='content' id='content11'>
    <div><p>answer_n</p></div>
    </div>

    <!-- Form 1.1n-->

    <div class='content' id='content6'>
    <p>This is Question 1.1n
    <br>'yes' will return Question 1.1.1y
    <br>'no' will return Question 1.1.1n</p>
    </div>
    <div class='info' id='info6'>
    <div><input type='radio' name='opt' value='9' />yes</div>
    <div><input type='radio' name='opt' value='10' />no</div>
    </div>


     <!-- Form 1.1.1n-->

    <div class='content' id='content10'>
    <p>This is Question 1.1.1n
    <br>'yes' will return answer_y
    <br>'no' will return answer_n</p>
    </div>
    <div class='info' id='info10'>
    <div><input type='radio' name='opt' value='4' />yes</div>
    <div><input type='radio' name='opt' value='11' />no</div>
    </div>







.info { display: none; }
.content { display: none; }

 var update = function() { 
 $(".info").hide();
 $("#info" + $(this).val()).show();
 };
 $("input[type='radio']").change(update);


 var update1 = function() { 
 $("#content" + $(this).val()).show();
 };
 $("input[type='radio']").change(update1);

http://jsfiddle.net/j2x96buL/15/

【问题讨论】:

  • 那个小提琴中的“是”和“否”在哪里?没有应该怎么办?
  • 什么价值观?有问题的代码和 jsfiddle 完全不同。我在小提琴中看不到任何是非选项。
  • 请停止更改代码。您正在使现有答案无效。这不是这里的工作方式......

标签: jquery forms radio-button


【解决方案1】:

使用复选框而不是单选框,这样您可以一次选择和取消选择多个。

然后,扩展您的函数以将每个表单的 id 传递给 hide 和 show 函数:

$('input[type="checkbox"]').change(function() {
  var val = $(this).val();
  $("#info" + val).show();
  if (!$(this).is(':checked')) {
    $("#info" + + val).hide();
  }
});

这是一个更新的小提琴: http://jsfiddle.net/j2x96buL/2/

【讨论】:

  • 感谢您的回答,恐怕我在编辑我的问题时回答了以前的版本,可能浪费了您的时间...请参考上面的编辑版本,无论如何谢谢
【解决方案2】:

问题在于您的HTML 结构。 (据此JSFiddle

你在另一个里面有.content。例如,#content3#content2 内部。

您的代码用于隐藏所有.content 并单独显示相应的代码。但是你看不到#content3,只要它的父 #content2 是隐藏的(例如)。

你如何展示隐藏在某物内部的东西?

所以你需要正确地重新构建你的HTML

旁注:您对同一事件的元素有多个处理程序,这可能会导致不可预测的行为。您实际上只需要一个,您可以在其中编写所有逻辑:

$("input[type='radio']").change(function () {
  // all the logic goes here
  // no need of a separate handler for doing something else
});

更新OP不断更改代码

根据最新的代码,你有 value10 的单选按钮,但是有 mo 匹配 #info10#content10 等。所以什么都没有显示。

【讨论】:

  • 是的@pizzasynthesis 和@T J,正如我上面所说,请接受我的道歉。我是新来的,所以我没有意识到人们会在我发布它后立即回答我的问题,所以我一直在调试和编辑,对不起,
猜你喜欢
  • 2011-08-31
  • 2017-07-24
  • 1970-01-01
  • 2011-09-22
  • 1970-01-01
  • 1970-01-01
  • 2016-06-17
相关资源
最近更新 更多