JQuery - Multiple Select Options

2020-02-02 04:37发布

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

<select id="ps-type" name="ps-type" multiple="multiple" size="5">
    <option>Residential - Wall Insulation</option>
    <option>Residential - Attic /Crawl Space Insulation</option>
    <option>Residential - Foundation Insulation</option>
    <option>Residential - Exterior Roof System</option>
    <option>Commercial - Wall Insulation</option>
    <option>Commercial - Air Barrier System (Walltite)</option>
    <option>Commercial - Roof System</option>
</select>

The result I am looking for is the following:

Residential - Wall Insulation, Commercial - Wall Insulation, ...

标签: jquery select
9条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-02-02 04:51

Something like this should do the trick:

var result = "";
$('#ps-type option:selected').each(function(i, item){ 
   result += $(this).val() + ", ";
});
查看更多
再贱就再见
3楼-- · 2020-02-02 04:58

The most simple solution

You could simply use just .val() like :

console.log( $('#ps-type').val() );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="ps-type" name="ps-type" multiple="multiple" size="5">
  <option selected>Residential - Wall Insulation</option>
  <option>Residential - Attic /Crawl Space Insulation</option>
  <option>Residential - Foundation Insulation</option>
  <option>Residential - Exterior Roof System</option>
  <option selected>Commercial - Wall Insulation</option>
  <option>Commercial - Air Barrier System (Walltite)</option>
  <option selected>Commercial - Roof System</option>
</select>

查看更多
Root(大扎)
4楼-- · 2020-02-02 05:01

On a multiple select element, the val command of the select element will return an array of the selected options values. If no values are present, the text of the element is used:

var output = $("#ps-type").val().join(', ');

update: However, when there are no selected options val() returns null not an empty array. One way to get around this:

var output = ($("#ps-type").val() || []).join(', '); 

You can play around with it in this demo I put together.

From the docs:

In the case of <select multiple="multiple"> elements, the .val() method returns an array containing each selected option.

查看更多
Luminary・发光体
5楼-- · 2020-02-02 05:02

You can use the :selected selector, and the inline $.map() function as part of your chain.

$("option:selected").map(function(){ return this.value }).get().join(", ");
查看更多
乱世女痞
6楼-- · 2020-02-02 05:03

$(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>

查看更多
▲ chillily
7楼-- · 2020-02-02 05:04

Here you go:

var result = new Array();

$("#ps-type option:selected").each(function() {
    result.push($(this).val());
});

var output = result.join(", ");
查看更多
登录 后发表回答