jQuery 如何获取 Select2 下拉列表中的所有值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16187672/
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 the values in a Select2 dropdown?
提问by sushil bharwani
How can we get all the elements that are in the jQuery Select2 dropdown plugin.
我们如何获取 jQuery Select2 下拉插件中的所有元素。
I have applied Select2 to a input type = hidden, and then populated it using Ajax.
我已将 Select2 应用于输入类型 = hidden,然后使用 Ajax 填充它。
Now at one instance I need to get all the values that are appearing in the dropdown.
现在,在一个实例中,我需要获取出现在下拉列表中的所有值。
Here is a input field.
这是一个输入字段。
<input type="hidden" id="individualsfront" name="individualsfront" style="width:240px" value="" data-spy="scroll" required />
and to this input field I have applied this
和这个输入字段我已经应用了这个
$("#individualsfront").select2({
multiple: true,
query: function (query){
var data = {results: []};
$.each(yuyu, function(){
if(query.term.length == 0 || this.text.toUpperCase().indexOf(query.term.toUpperCase()) >= 0 ){
data.results.push({id: this.id, text: this.text });
}
});
query.callback(data);
}
});
The yuyu is a json coming from some AJAX call and populating the Select2.
yuyu 是来自某个 AJAX 调用并填充 Select2 的 json。
Now I want in some other code a way to get all the values inside the Select2.
现在我想在其他一些代码中使用一种方法来获取 Select2 中的所有值。
采纳答案by Ouadie
option 1:you can use directly the data
object
选项 1:您可以直接使用data
对象
var results = [];
$("#individualsfront").select2({
multiple: true,
query: function (query){
var data = {results: []};
$.each(yuyu, function(){
if(query.term.length == 0 || this.text.toUpperCase().indexOf(query.term.toUpperCase()) >= 0 ){
data.results.push({id: this.id, text: this.text });
}
});
results = data.results; //<=====
query.callback(data);
}
});
and use it like this for example:
并像这样使用它,例如:
$('#individualsfront').on('open',function(){
$.each(results, function(key,value){
console.log("text:"+value.text);
});
});
option 2:request the feature and use (temporary) something like:
选项 2:请求该功能并使用(临时)类似的东西:
$('#individualsfront').on('open',function(){
$(".select2-result-label").each(function()
{
console.log($(this).text());
})
});
});
http://jsfiddle.net/ouadie/qfchH/1/option 3:you can use .select2("data");
but it returns all elements only if there is no selected element.
http://jsfiddle.net/ouadie/qfchH/1/选项 3:您可以使用,.select2("data");
但只有在没有选定元素时才返回所有元素。
var arrObj = $("#individualsfront").select2("data");
for each(yuyu in arrObj)
{
console.log("id: "+yuyu.id+ ", value: "+yuyu.text);
}
var arrObj = $("#individualsfront").select2("data");
for each(yuyu in arrObj)
{
console.log("id: "+yuyu.id+ ", value: "+yuyu.text);
}
回答by Alao
I was trying to figure this out myself and found that you can get at least the options in a select2.
我试图自己解决这个问题,发现您至少可以在 select2 中获得选项。
var options = $("#dropdown").find("option")
var optionsText = $("#dropdown").find("option[value='1']").text()
回答by Mariusz Charczuk
function select2Options($elementSelect2) {
var data = [],
adapter = $elementSelect2.data().select2.dataAdapter;
$elementSelect2.children().each(function () {
if (!$(this).is('option') && !$(this).is('optgroup')) {
return true;
}
data.push(adapter.item($(this)));
});
return data;
}