HTML PHP JS选择选择不同的选项以显示不同的复选框

时间:2018-05-14 03:53:10

标签: javascript php

我想制作一个包含select的表单。根据不同的选项,将显示不同的复选框以供进一步选择。

我曾经隐藏并显示JS代码,但它不适合用户选择所有复选框(用户可以选择球类游戏和水上游戏)。如何避免让用户选中所有复选框?

$(function() {
  $('#sort').change(function(){
    $('.sort2').hide();
    $('#' + $(this).val()).load();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form " method="post" id="s-form"> 
	<div class="form-group">
		<label for="sort">Sort</label>
            <select class="form-control" id="sort">
                <option value="ball">Ball game</option>
                <option value="water">Water game</option>
            </select>
     </div>
</form>
  
    <div class="row" id="ball" class="sort2 ball" style="display: none">
        <input type="checkbox" name="sort2" value="football" > Football
        <input type="checkbox" name="sort2" value="basketball" > Basketball
    </div>
  
    <div class="row" id="water" class="sort2 water" style="display: none">
        <input type="checkbox" name="sort2" value="swimming" checked="checked"> Swimming
        <input type="checkbox" name="sort2" value="diving" > Diving
    </div>

3 个答案:

答案 0 :(得分:1)

$(function() {
  $('#sort').change(function(){
    $('.sort2').hide();
    $('#' + $(this).val()).show();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form " method="post" id="s-form"> 
	<div class="form-group">
		<label for="sort">Sort</label>
            <select class="form-control" id="sort">
                <option value="ball">Ball game</option>
                <option value="water">Water game</option>
            </select>
     </div>
</form>
  
    <div class="row sort2 ball" id="ball" style="display: none">
        <input type="checkbox" name="sort2" value="football" > Football
        <input type="checkbox" name="sort2" value="basketball" > Basketball
    </div>
  
    <div class="row sort2 water" id="water"  style="display: none">
        <input type="checkbox" name="sort2" value="swimming" checked="checked"> Swimming
        <input type="checkbox" name="sort2" value="diving" > Diving
    </div>

答案 1 :(得分:0)

你也可以这样做:

&#13;
&#13;
$(function() {
  $('#sort').change(function(){
    //$('.sort2').hide();
    $('select option').each(function(){
      $('#' + $(this).val()).hide();
    });
    $('#' + $(this).val()).show();
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form " method="post" id="s-form"> 
	<div class="form-group">
		<label for="sort">Sort</label>
            <select class="form-control" id="sort">
                <option value="ball">Ball game</option>
                <option value="water">Water game</option>
            </select>
     </div>
</form>
  
    <div class="row" id="ball" class="sort2 ball" style="display: none">
        <input type="checkbox" name="sort2" value="football" > Football
        <input type="checkbox" name="sort2" value="basketball" > Basketball
    </div>
  
    <div class="row" id="water" class="sort2 water" style="display: none">
        <input type="checkbox" name="sort2" value="swimming" checked="checked"> Swimming
        <input type="checkbox" name="sort2" value="diving" > Diving
    </div>
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

删除style="display: none" 来自div标签 并将.load()函数替换为.show()

$(function() {
  $('.sort2').hide();
  $('#sort').change(function(){
    $('.sort2').hide();
    $('#' + $(this).val()).show();
  });
});