JQuery - Multiple Select Options

前端 未结 9 713
时光取名叫无心
时光取名叫无心 2020-12-07 15:53

I am trying to grab all selected items in the following select multiple and separate them by a comma. The code is below:


                        
    
提交评论

  • 2020-12-07 16:30

    Something like this should do the trick:

    var result = "";
    $('#ps-type option:selected').each(function(i, item){ 
       result += $(this).val() + ", ";
    });
    
    0 讨论(0)
  • 2020-12-07 16:31

    $(function() {
            $('#colorselector').change(function(){
                $('.colors').hide();
                $('#' + $(this).val()).show();
            });
        });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <Select id="colorselector" multiple="multiple" size="2">
       <option value="red">Red</option>
       <option value="yellow">Yellow</option>
       <option value="blue">Blue</option>
    </Select>
    <div id="red" class="colors" style="display:none"> red... </div>
    <div id="yellow" class="colors" style="display:none"> yellow.. </div>
    <div id="blue" class="colors" style="display:none"> blue.. </div>

    0 讨论(0)
  • 提交回复
    热议问题