jQuery:将选中的文本作为逗号与选择框分开

时间:2022-04-22 21:47:02
<select id="testID" multiple="multiple">
   <option value="1">test Value1</option>
   <option value="2">test Value2</option>
   <option value="3">test Value3</option>
   <option value="4">test Value4</option>
   <option value="5">test Value5</option>
  <option value="6">test Value6</option>
</select>

<input type="button" value="Get dropdown selected Value" id ="select-values">

i want to get the comma separated selected text.

我想让逗号分隔选定的文本。

I tried ,

我试过了 ,

 $("#select-values").click(function () {
     $("select option:selected").text();
});

lets say i have selected first two select options but i am getting the value test Value1test Value2

让我说我已经选择了前两个选择选项,但我得到了值测试Value1test Value2

i want it to be test Value1,test Value2

我希望它是测试Value1,测试Value2

7 个解决方案

#1


34  

Try

fiddle demo

$("#select-values").click(function () {
    var option_all = $("select option:selected").map(function () {
        return $(this).text();
    }).get().join(',');
    console.log(option_all);
});

#2


4  

This feels like the quickest way:

这感觉就像最快的方式:

$('#testID').val().toString()

I'm too lazy to write a for loop for this

我懒得为此写一个for循环

#3


1  

Try something like this

尝试这样的事情

$(document).ready(function () {
    $("#select-values").click(function () {
        var val = [];
        $("select option:selected").each(function () {
            val.push(this.text);
        });
        alert(val.join(','));
    });
});

#4


1  

Did you know when you want to get a value of a multiple select box, it does this automatically? ^^

你知道什么时候想要获得多个选择框的值,它会自动执行此操作吗? ^^

Just change the value of your selections:

只需更改您选择的值:

<select id="testID" multiple="multiple">
   <option value="test Value1">test Value1</option>
   <option value="test Value2">test Value2</option>
   <option value="test Value3">test Value3</option>
   <option value="test Value4">test Value4</option>
   <option value="test Value5">test Value5</option>
  <option value="test Value6">test Value6</option>
</select>

Then you can call the value with:

然后你可以用以下方法调用该值:

$('#testID').val();

It will automatically seperate the values with a comma.

它将使用逗号自动分隔值。

jsFiddle

#5


1  

Try:

$("#select-values").click(function () {
    var select = "";
    $("select option:selected").each(function(){
        select += ","+$(this).text();
    });
    if(select != ""){
        select = select.substr(1);
    }
    $("#selection").text(select);
});

Fiddle here.

#6


1  

Try:

var selectedItems = ($.map($("select[id*=select-values] option:selected"), function(item, i) { return $(item).text() })).join(", ");

var selectedItems =($ .map($(“select [id * = select-values] option:selected”),function(item,i){return $(item).text()}))。join(“, “);

#7


0  

Keep it simple

把事情简单化

$('#testID').val().join(', ');

#1


34  

Try

fiddle demo

$("#select-values").click(function () {
    var option_all = $("select option:selected").map(function () {
        return $(this).text();
    }).get().join(',');
    console.log(option_all);
});

#2


4  

This feels like the quickest way:

这感觉就像最快的方式:

$('#testID').val().toString()

I'm too lazy to write a for loop for this

我懒得为此写一个for循环

#3


1  

Try something like this

尝试这样的事情

$(document).ready(function () {
    $("#select-values").click(function () {
        var val = [];
        $("select option:selected").each(function () {
            val.push(this.text);
        });
        alert(val.join(','));
    });
});

#4


1  

Did you know when you want to get a value of a multiple select box, it does this automatically? ^^

你知道什么时候想要获得多个选择框的值,它会自动执行此操作吗? ^^

Just change the value of your selections:

只需更改您选择的值:

<select id="testID" multiple="multiple">
   <option value="test Value1">test Value1</option>
   <option value="test Value2">test Value2</option>
   <option value="test Value3">test Value3</option>
   <option value="test Value4">test Value4</option>
   <option value="test Value5">test Value5</option>
  <option value="test Value6">test Value6</option>
</select>

Then you can call the value with:

然后你可以用以下方法调用该值:

$('#testID').val();

It will automatically seperate the values with a comma.

它将使用逗号自动分隔值。

jsFiddle

#5


1  

Try:

$("#select-values").click(function () {
    var select = "";
    $("select option:selected").each(function(){
        select += ","+$(this).text();
    });
    if(select != ""){
        select = select.substr(1);
    }
    $("#selection").text(select);
});

Fiddle here.

#6


1  

Try:

var selectedItems = ($.map($("select[id*=select-values] option:selected"), function(item, i) { return $(item).text() })).join(", ");

var selectedItems =($ .map($(“select [id * = select-values] option:selected”),function(item,i){return $(item).text()}))。join(“, “);

#7


0  

Keep it simple

把事情简单化

$('#testID').val().join(', ');