根据无线电选择显示/隐藏表单内容

时间:2013-05-02 00:30:47

标签: javascript jquery html css

我正在构建一个包含条件字段的表单,需要根据选中的复选框进行显示。我想知道如何在选中复选框时隐藏其他表单字段。我还希望能够在选中两个复选框时显示所有字段。

在下面的标记中,我添加了我想要呈现html的注释。

<form class="form-horizontal">
<fieldset>


<!-- Multiple Checkboxes -->
<div class="control-group">
  <label class="control-label">Multiple Checkboxes</label>
  <div class="controls">
    <label class="checkbox">
      <input type="checkbox" name="checkboxes" value="Load">
      Load
    </label>
    <label class="checkbox">
      <input type="checkbox" name="checkboxes" value="Unload">
      Unload
    </label>
  </div>
</div>

<!-- Show when "load" is checked -->
<div class="control-group">
  <label class="control-label">Dorms</label>
  <div class="controls">
    <select id="selectbasic" name="selectbasic" class="input-xlarge">
      <option>Degraff</option>
    </select>
  </div>
</div>

<!-- Show when "load" is checked -->
<div class="control-group">
  <label class="control-label">Greek House</label>
  <div class="controls">
    <select id="selectbasic" name="selectbasic" class="input-xlarge">
      <option>Tri Delt</option>
    </select>
  </div>
</div>

<!-- Show when "unload" is checked -->
<div class="control-group">
  <label class="control-label">How many hops?</label>
  <div class="controls">
    <select id="selectbasic" name="selectbasic" class="input-xlarge">
      <option>One</option>
      <option>Two</option>
      <option>Three</option>
      <option>Four</option>
    </select>
  </div>
</div>

<!-- Show when "unload" is checked -->
<div class="control-group">
  <label class="control-label">How many hours?</label>
  <div class="controls">
    <select id="selectbasic" name="selectbasic" class="input-xlarge">
      <option>One</option>
      <option>Two</option>
    </select>
  </div>
</div>

</fieldset>
</form>

2 个答案:

答案 0 :(得分:3)

我在您的元素中添加了一些id属性。

<强> HTML

<form class="form-horizontal">
<fieldset>


<!-- Multiple Checkboxes -->
<div class="control-group">
  <label class="control-label">Multiple Checkboxes</label>
  <div class="controls">
    <label class="checkbox">
      <input type="checkbox" name="checkboxes" value="Load">
      Load
    </label>
    <label class="checkbox">
      <input type="checkbox" name="checkboxes" value="Unload">
      Unload
    </label>
  </div>
</div>

<!-- Show when "load" is checked -->
<div id="dorms" class="control-group">
  <label class="control-label">Dorms</label>
  <div class="controls">
    <select id="selectbasic" name="selectbasic" class="input-xlarge">
      <option>Degraff</option>
    </select>
  </div>
</div>

<!-- Show when "load" is checked -->
<div id="greek" class="control-group">
  <label class="control-label">Greek House</label>
  <div class="controls">
    <select id="selectbasic" name="selectbasic" class="input-xlarge">
      <option>Tri Delt</option>
    </select>
  </div>
</div>

<!-- Show when "unload" is checked -->
<div id="hops" class="control-group">
  <label class="control-label">How many hops?</label>
  <div class="controls">
    <select id="selectbasic" name="selectbasic" class="input-xlarge">
      <option>One</option>
      <option>Two</option>
      <option>Three</option>
      <option>Four</option>
    </select>
  </div>
</div>

<!-- Show when "unload" is checked -->
<div id="hours" class="control-group">
  <label class="control-label">How many hours?</label>
  <div class="controls">
    <select id="selectbasic" name="selectbasic" class="input-xlarge">
      <option>One</option>
      <option>Two</option>
    </select>
  </div>
</div>

</fieldset>
</form>

<强>的jQuery

$("input[value='Load']").click(function(){
    if($(this).is(":checked")){
       $("#dorms, #greek").show();
    }else{
        $("#dorms, #greek").hide();
    }
});

    $("input[value='Unload']").click(function(){
        if($(this).is(":checked")){
           $("#hops, #hours").show();
        }else{
            $("#hops, #hours").hide();
        }
    });

<强> CSS

#greek, #dorms, #hops, #hours{
    display:none;
}

工作示例 http://jsfiddle.net/cyTLm/

答案 1 :(得分:2)

可能这可以为您提供有关您需求的指示。你可以探索更多。我在你的html中做了一些小改动并使用了jquery(因为你已经标记了)和一点css。

查看小提琴 -

.toggle()

<强> http://jsfiddle.net/kvaYG/

<强> Jquery的

$(':checkbox[value=Load]').change(function(){
    $('.load').toggle(); //This will get triggered during the checkbox change 
//i.e when you check or unckeck the checkbox. You can use element.toggle() 
//which will show and hide based on its last appearance whether it was shown or not. 
//This is a shortcut to using .show() and .hide()
});
    $(':checkbox[value=Unload]').change(function(){
      $('.unload').toggle();
});

<强>的CSS

/*Initially hide all your sections*/
.load
{
    display:none;
}
.unload
{
    display:none;
}