如何使用jquery获得多个选择框值?

时间:2021-11-08 21:19:17

How to get multiple select box values using jquery?

如何使用jquery获得多个选择框值?

7 个解决方案

#1


133  

jQuery .val()

jQuery .val()

  var foo = $('#multiple').val(); 

#2


269  

Using the .val() function on a multi-select list will return an array of the selected values:

在多选择列表中使用.val()函数将返回所选值的数组:

var selectedValues = $('#multipleSelect').val();

and in your html:

在你的html:

<select id="multipleSelect" multiple="multiple">
    <option value="1">Text 1</option>
    <option value="2">Text 2</option>
    <option value="3">Text 3</option>
</select>

#3


10  

You can also use js map function:

也可以使用js map函数:

$("#multipleSelect :selected").map(function(i, el) {
    return $(el).val();
});

And then you can get any property of the option element:

然后你可以得到选项元素的任何属性:

return $(el).text();
return $(el).data("mydata");
return $(el).prop("disabled");
etc...

#4


5  

var selected=[];
 $('#multipleSelect :selected').each(function(){
     selected[$(this).val()]=$(this).text();
    });
console.log(selected);

Yet another approch to this problem. The selected array will have the indexes as the option values and the each array item will have the text as its value.

这又是一个解决这个问题的方法。选择的数组将索引作为选项值,每个数组项将文本作为其值。

for example

例如

<select id="multipleSelect" multiple="multiple">
    <option value="abc">Text 1</option>
    <option value="def">Text 2</option>
    <option value="ghi">Text 3</option>
</select>

if say option 1 and 2 are selected.

如果选择选项1和2。

the selected array will be :

所选数组为:

selected['abc']=1; 
selected['def']=2.

#5


2  

Html Code:

Html代码:

 <select id="multiple" multiple="multiple" name="multiple">
  <option value=""> -- Select -- </option>
  <option value="1">Opt1</option>
  <option value="2">Opt2</option>
  <option value="3">Opt3</option>
  <option value="4">Opt4</option>
  <option value="5">Opt5</option>
 </select>   

JQuery Code:

JQuery代码:

$('#multiple :selected').each(function(i, sel){ 
    alert( $(sel).val() ); 

});

Hope it works

希望它的工作原理

#6


0  

Get selected values in comma separator

获取逗号分隔符中选定的值

var Accessids = "";
$(".multi_select .btn-group>ul>li input:checked").each(function(i,obj)
{
    Accessids=Accessids+$(obj).val()+",";
});
Accessids = Accessids.substring(0,Accessids.length - 1);
console.log(Accessids);

#7


0  

Just use this

就用这个

$('#multipleSelect').change(function() {
    var selectedValues = $(this).val();  
});

#1


133  

jQuery .val()

jQuery .val()

  var foo = $('#multiple').val(); 

#2


269  

Using the .val() function on a multi-select list will return an array of the selected values:

在多选择列表中使用.val()函数将返回所选值的数组:

var selectedValues = $('#multipleSelect').val();

and in your html:

在你的html:

<select id="multipleSelect" multiple="multiple">
    <option value="1">Text 1</option>
    <option value="2">Text 2</option>
    <option value="3">Text 3</option>
</select>

#3


10  

You can also use js map function:

也可以使用js map函数:

$("#multipleSelect :selected").map(function(i, el) {
    return $(el).val();
});

And then you can get any property of the option element:

然后你可以得到选项元素的任何属性:

return $(el).text();
return $(el).data("mydata");
return $(el).prop("disabled");
etc...

#4


5  

var selected=[];
 $('#multipleSelect :selected').each(function(){
     selected[$(this).val()]=$(this).text();
    });
console.log(selected);

Yet another approch to this problem. The selected array will have the indexes as the option values and the each array item will have the text as its value.

这又是一个解决这个问题的方法。选择的数组将索引作为选项值,每个数组项将文本作为其值。

for example

例如

<select id="multipleSelect" multiple="multiple">
    <option value="abc">Text 1</option>
    <option value="def">Text 2</option>
    <option value="ghi">Text 3</option>
</select>

if say option 1 and 2 are selected.

如果选择选项1和2。

the selected array will be :

所选数组为:

selected['abc']=1; 
selected['def']=2.

#5


2  

Html Code:

Html代码:

 <select id="multiple" multiple="multiple" name="multiple">
  <option value=""> -- Select -- </option>
  <option value="1">Opt1</option>
  <option value="2">Opt2</option>
  <option value="3">Opt3</option>
  <option value="4">Opt4</option>
  <option value="5">Opt5</option>
 </select>   

JQuery Code:

JQuery代码:

$('#multiple :selected').each(function(i, sel){ 
    alert( $(sel).val() ); 

});

Hope it works

希望它的工作原理

#6


0  

Get selected values in comma separator

获取逗号分隔符中选定的值

var Accessids = "";
$(".multi_select .btn-group>ul>li input:checked").each(function(i,obj)
{
    Accessids=Accessids+$(obj).val()+",";
});
Accessids = Accessids.substring(0,Accessids.length - 1);
console.log(Accessids);

#7


0  

Just use this

就用这个

$('#multipleSelect').change(function() {
    var selectedValues = $(this).val();  
});