根据单选框显示/隐藏DIV部分

时间:2016-09-12 21:17:29

标签: jquery html css forms

我试图重做几种表格,有几个部分我有一个典型的"是否需要xxxx?"是/否收音机盒。如果他们选择是,我希望下面的框打开,要求他们输入更多信息。我希望使用一个JQuery函数,如果我可以使用它,而不是每个需要它的问题的单独函数。

我也有一个问题,他们选择涉及的人是员工还是客人。感谢这里有很多好帖子,我能够想出隐藏客人DIV直到被选中,但我还没弄清楚如何为员工DIV做同样的事情,我发现我需要添加一个供应商的第3组问题。

这是显示访客部分的工作代码

$(document).ready(function() {
   $("div.desc").hide();
   $("input[name$='Victim']").click(function() {
    var test = $(this).val();
      $("div.desc").hide();
      $("#" + test).show();
  });
});

一起
<p>
   Who was involved in this incident? &nbsp &nbsp
    <input name="Victim" type="radio" value="Guest" required >Guest 
     &nbsp; &nbsp;
    <input name="Victim" type="radio" value="Employee" > Employee 
    &nbsp; &nbsp;

  <div id="Guest" class="desc">
  <p>
     Some questions here ...

有人能指出我的其他代码是否正常工作? TIA

1 个答案:

答案 0 :(得分:0)

不清楚你的意思..如果你有一个id为Employee的div和一个Vendor的div,并添加了供应商的单选按钮....代码工作正常.... < / p>

$(document).ready(function() {
   $("div.desc").hide();
   $("input[name$='Victim']").click(function() {
    var test = $(this).val();
      $("div.desc").hide();
      $("#" + test).show();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h3> Who was involved in this incident? </h3>

<p><label><input name="Victim" type="radio" value="Guest" required >Guest </label></p>

<p><label><input name="Victim" type="radio" value="Employee" > Employee </label</p>
  
  <p><label><input name="Victim" type="radio" value="Vendor" > Vendor</label></p>
  

  <div id="Guest" class="desc">
  <p><strong>Guest</strong> questions here ...</p>
   </div>

  <div id="Employee" class="desc">
  <p><strong>Employee</strong> questions here ...</p>
   </div>

  <div id="Vendor" class="desc">
  <p><strong>Vendor</strong> questions here ...</p>
   </div>