简体   繁体   中英

Show/hide div select multiple

I have the following code: http://jsfiddle.net/tucuta/pvvmvdpg/

 $("#mySelect").change(function(){ $("#div1").fadeIn("fast")[ ($(this).val() == 'value1') ? 'show' : 'hide' ](); $("#div2").fadeIn("fast")[ ($(this).val() == 'value2') ? 'show' : 'hide' ](); }); $("#mySelect").change(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="mySelect" multiple="multiple"> <option value="value1">First</option> <option value="value2">Second</option> <option value="value3">Third</option> <option value="value4">Fourth</option> </select> <div id="div1">Div 1</div> <div id="div2">Div 2</div> 

The code works fine if it is for a normal select, but if it is for a multiple select not working.

If I select for example first shows me div 1, but if I select first and then second, it shows me neither div1 nor div2.

Someone please help me, I thank you in advance

Check that the value you're looking for is anywhere in the list, via $.inArray()

 $("#mySelect").change(function(){ $("#div1").fadeIn("fast")[ $.inArray('value1', $(this).val()) >= 0 ? 'show' : 'hide' ](); $("#div2").fadeIn("fast")[ $.inArray('value2', $(this).val()) >= 0 ? 'show' : 'hide' ](); }); $("#mySelect").change(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="mySelect" multiple="multiple"> <option value="value1">First</option> <option value="value2">Second</option> <option value="value3">Third</option> <option value="value4">Fourth</option> </select> <div id="div1">Div 1</div> <div id="div2">Div 2</div> 

Just remove this line

$("#mySelect").change();

and it will work because you have used the change event on '#myselect' and it will fire automatically, you dont have to call it.

Use

$.inArray(value, array) // Return the index in array or -1 if not exists
if( $.inArray('value1', $(this).val()) != -1 ) { ... }

I have added few lines. Its working Code fiddle

HTML

<select id="mySelect" multiple="multiple">
  <option value="value1">First</option>
  <option value="value2">Second</option>
  <option value="value3">Third</option>
  <option value="value4">Fourth</option>
</select>
<div id="div1" class="a">Div 1</div>
<div id="div2" class="a">Div 2</div>
<div id="div3" class="a">Div 3</div>
<div id="div4" class="a">Div 4</div>

SCRIPT

$(document).ready(function() {
    $("#mySelect").change(function(){
        $(".a").hide();                              
        if( $(this).val()){
            for(var i=0; i < $(this).val().length; i++){
                if($(this).val()[i] == "value1"){
                        $("#div1").fadeIn("fast")['show']();
                }
                else if($(this).val()[i] == "value2"){
                        $("#div2").fadeIn("fast")['show']();
                }
                 else if($(this).val()[i] == "value3"){
                        $("#div3").fadeIn("fast")['show']();
                }
                 else if($(this).val()[i] == "value4"){
                        $("#div4").fadeIn("fast")['show']();
                }
            }
        }
    });
    $("#mySelect").change();
});

One possible solution could be this one:

HTML :

<select id="mySelect" multiple="multiple">
  <option value="value1">First</option>
  <option value="value2">Second</option>
  <option value="value3">Third</option>
  <option value="value4">Fourth</option>
</select>
<div id="div1" match="value1">Div 1</div>
<div id="div2" match="value2">Div 2</div>
<div id="div3" match="value3">Div 3</div>
<div id="div4" match="value4">Div 4</div>

CSS :

div{
    display:none;
}

jQuery :

$(document).ready(function() {
    function exists(elemt,arr){
        return (jQuery.inArray(elemt,arr) > -1);
    }

    $('#mySelect').change(function(){
        var $selected = $(this).val();
        $('div').each(function(){
                $(this).fadeIn('fast')[ 
                    (exists($(this).attr('match'),$selected)) ? 'show' : 'hide' ]();      
        });
    });
});

Check this link jsfiddle to see a working example.

Hope it's useful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM