jQuery 如何通过Jquery获取“选择”中的所有“选项”值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1563459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get all "option" values in "select" by Jquery?
提问by Jin Yong
Does anyone know how can I get all values in the select by Jquery?
有谁知道如何通过 Jquery 获取选择中的所有值?
Example:
例子:
<select name="group_select" id="group_select">
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
Which I want to get all option values (a,b and c) from select (without selected any option) by using jquery
我想通过使用 jquery 从选择(没有选择任何选项)中获取所有选项值(a、b 和 c)
回答by eulerfx
var values = $("#group_select>option").map(function() { return $(this).val(); });
回答by Russ Cam
The following will give you an array of the <option>
values
以下将为您提供一组<option>
值
var values = $.map($('#group_select option'), function(e) { return e.value; });
// as a comma separated string
values.join(',');
Here's a Working Demo. add /editto the URL to see the code.
这是一个工作演示。将/edit添加到 URL 以查看代码。
回答by helloandre
This will iterate through all the options and give you their values.Then you can either append them to an array or manipulate them individually.
这将遍历所有选项并为您提供它们的值。然后您可以将它们附加到数组或单独操作它们。
$('#group_select option').each(function(){
$(this).val()
};
回答by Mitul Maheshwari
You can use following code for that :-
您可以使用以下代码:-
var assignedRoleId = new Array();
$('#RolesListAssigned option').each(function(){
assignedRoleId.push(this.value);
assignedRoleId.push(this.text);
});