选择显示/隐藏div

时间:2014-11-17 14:21:09

标签: javascript

我试图根据选择框所在的值显示div。使用我当前的脚本它正在工作,但是,当我更改select的值时,它显示下一个div而不隐藏先前选择的那个。我只希望这个JS脚本显示当前选中的div,而不是每个选择框选择的div(即从一个选项切换到另一个选项)。

js和html

 <script>
        $(function() {
            $('#contract_bid').hide();
            $('#equipment_purchase').hide();
            $('#hiring_employees').hide();
            $('#marketing').hide();
            $('#expansion').hide();
            $('#working_capital').hide();
            $('#inventory_purchase').hide();
            $('#refinancing').hide();
            $('#other').hide();
            $('#loan_application_requested_purpose').change(function(){
                if($('#loan_application_requested_purpose').val() == 'Contract Bid') {
                    $('#contract_bid').show(); 
                } else if($('#loan_application_requested_purpose').val() == 'Equipment Purchase') {
                    $('#equipment_purchase').show(); 
                } else if($('#loan_application_requested_purpose').val() == 'Hiring Employees') {
                    $('#hiring_employees').show(); 
                } else if($('#loan_application_requested_purpose').val() == 'Marketing') {
                    $('#marketing').show(); 
                } else if($('#loan_application_requested_purpose').val() == 'Expansion/Renovation') {
                    $('#expansion').show(); 
                } else if($('#loan_application_requested_purpose').val() == 'Working Capital') {
                    $('#working_capital').show(); 
                } else if($('#loan_application_requested_purpose').val() == 'Inventory Purchase') {
                    $('#inventory_purchase').show(); 
                } else if($('#loan_application_requested_purpose').val() == 'Refinancing') {
                    $('#refinancing').show(); 
                } else {
                    $('#other').show(); 
                }
            });
        });
    </script>

# HTML
<div id="contract_bid"></div>
<div id="equipment_purchase"></div>
<div id="hiring_employees"></div>
<div id="marketing"></div>
<div id="expansion"></div>
<div id="working_capital"></div>
<div id="inventory_purchase"></div>
<div id="refinancing"></div>
<div id="other"></div>

6 个答案:

答案 0 :(得分:2)

喜欢这个

  1. 给所有div一个类,例如“loan_purpose”
  2. 将您的脚本更改为:
  3. $(function() {
      $('#loan_application_requested_purpose').on("change",function(){
        $(".loan_purpose").hide();
        // change all spaces to underscore and grab the first part of Expansion/
        var $div = $("#"+$(this).val().toLowerCase().replace(/ /g,"_").split("/")[0]);
        if ($div.length>0) $div.show();
        else $("#other").show(); 
      }).change(); // run change on load to show relevant already selected
    });
    

    提供课程的替代方法:如果你的div有一个共同的父母,你可以做

    $("#parentID > div").hide(); 
    

    而不是

    $(".loan_purpose").hide();
    

    替代解决方案:

    如果您可以更改值以反映要显示的div的ID,那么脚本将会更短:

    <select id="loan_application_requested_purpose">
      <option value="other">Please select</option>
      <option value="equipment_purchase">Equipment Purchase</option>
      .
      .
      <option value="expansion">Expansion/Renovation</option>
    </select>
    

    然后我的脚本只需要

    $(function() {
        $('#loan_application_requested_purpose').on("change",function(){
          $(".loan_purpose").hide();
          $("#"+$(this).val()).show();
        }).change(); // run change on load to show relevant already selected
    });
    

答案 1 :(得分:0)

首先为所有div添加公共类:

<div id="other" class="section"></div>
<div id="contract_bid" class="section"></div>
<!-- and so on ... -->

然后它将允许您减少代码:

$(function () {
    $('.section').hide();

    $('#loan_application_requested_purpose').change(function () {

        $('.section').hide();

        var val = $(this).val();

        if (val == 'Contract Bid') {
            $('#contract_bid').show();
        } else if (val == 'Equipment Purchase') {
            $('#equipment_purchase').show();
        }
        // other checks ....
        else {
            $('#other').show();
        }
    });
});

请注意,使用var val = $(this).val();仅计算一次值更清晰,稍后再使用。

最后更好的方法是使用CSS初始隐藏部分(并摆脱第一个$('.section').hide();):

.section {
    display: none;
}

答案 2 :(得分:0)

<div id="#myDivs">
<div id="contract_bid"></div>
<div id="equipment_purchase"></div>
<div id="hiring_employees"></div>
<div id="marketing"></div>
<div id="expansion"></div>
<div id="working_capital"></div>
<div id="inventory_purchase"></div>
<div id="refinancing"></div>
<div id="other"></div>
</div>

    if($('#loan_application_requested_purpose').val() == 'Contract Bid') {
         $("#myDivs div").hide()                    
        $('#contract_bid').show(); 
    }else
    {
    ...

答案 3 :(得分:0)

你可以在显示隐藏的div之前隐藏可见的div:

    $(function() {
        $('#contract_bid').hide();
        $('#equipment_purchase').hide();
        $('#hiring_employees').hide();
        $('#marketing').hide();
        $('#expansion').hide();
        $('#working_capital').hide();
        $('#inventory_purchase').hide();
        $('#refinancing').hide();
        $('#other').hide();
        $('#loan_application_requested_purpose').change(function(){
            // here comes the change:
            $('.loan_application_purpose:visible').hide();
            if($('#loan_application_requested_purpose').val() == 'Contract Bid') {
                $('#contract_bid').show(); 
            } else if($('#loan_application_requested_purpose').val() == 'Equipment Purchase') {
                $('#equipment_purchase').show(); 
            } else if($('#loan_application_requested_purpose').val() == 'Hiring Employees') {
                $('#hiring_employees').show(); 
            } else if($('#loan_application_requested_purpose').val() == 'Marketing') {
                $('#marketing').show(); 
            } else if($('#loan_application_requested_purpose').val() == 'Expansion/Renovation') {
                $('#expansion').show(); 
            } else if($('#loan_application_requested_purpose').val() == 'Working Capital') {
                $('#working_capital').show(); 
            } else if($('#loan_application_requested_purpose').val() == 'Inventory Purchase') {
                $('#inventory_purchase').show(); 
            } else if($('#loan_application_requested_purpose').val() == 'Refinancing') {
                $('#refinancing').show(); 
            } else {
                $('#other').show(); 
            }
        });
    });

HTML:

<div id="contract_bid" class="loan_application_purpose"></div>
<div id="equipment_purchase" class="loan_application_purpose"></div>
<div id="hiring_employees" class="loan_application_purpose"></div>
<div id="marketing" class="loan_application_purpose"></div>
<div id="expansion" class="loan_application_purpose"></div>
<div id="working_capital" class="loan_application_purpose"></div>
<div id="inventory_purchase" class="loan_application_purpose"></div>
<div id="refinancing" class="loan_application_purpose"></div>
<div id="other" class="loan_application_purpose"></div>

答案 4 :(得分:0)

始终隐藏所有然后显示所选:

$( '#trigger' ).change( function() {
  var selected_id = '#' + $('#trigger').val(); // Grab the ID to show
  $( '.foo' ).hide();                          // Hide all
  $( selected_id ).show();                     // Show selected
} ).change();                                  // Run at least once
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="trigger">
  <option>d1</option>
  <option>d2</option>
</select>

<div id="d1" class="foo">D1</div>
<div id="d2" class="foo">D2</div>

答案 5 :(得分:-2)

在这里,你必须在if或elseif条件中隐藏其他div,如if

if($('#loan_application_requested_purpose').val() == 'Contract Bid') {

 $('#contract_bid').show(); 
    $('#equipment_purchase').hide();
    $('#hiring_employees').hide();
    $('#marketing').hide();
    $('#expansion').hide();
    $('#working_capital').hide();
    $('#inventory_purchase').hide();
    $('#refinancing').hide();
}