如何获得在选择二下拉列表中的所有值? [英] How to get all the values in a Select2 dropdown?
问题描述
我们如何能够得到所有的都在jQuery的选择二下拉插件的元素。
How can we get all the elements that are in the jQuery Select2 dropdown plugin.
我已经申请选择二到输入类型=隐藏,然后填充它使用Ajax。
I have applied Select2 to a input type = hidden, and then populated it using Ajax.
现在的一个实例,我需要把所有的都出现在下拉列表中的值。
Now at one instance I need to get all the values that are appearing in the dropdown.
下面是一个输入字段。
<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);
}
});
该俞渝是一个JSON一些AJAX调用来来去填充选择二。
The yuyu is a json coming from some AJAX call and populating the Select2.
现在我想在其他一些codeA的方式来获得所有选择二中的值。
Now I want in some other code a way to get all the values inside the Select2.
推荐答案
选项1::您可以直接使用了数据
对象
option 1: you can use directly the data
object
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);
});
});
选项2:请求的功能和使用(临时)是这样的:
option 2: request the feature and use (temporary) something like:
$('#individualsfront').on('open',function(){
$(".select2-result-label").each(function()
{
console.log($(this).text());
})
});
});
http://jsfiddle.net/ouadie/qfchH/1/
<击> 选项3::您可以使用 .select2(数据);
但它返回所有元素只有在没有选择的元素。
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.
var arrObj = $("#individualsfront").select2("data");
for each(yuyu in arrObj)
{
console.log("id: "+yuyu.id+ ", value: "+yuyu.text);
}
击>
这篇关于如何获得在选择二下拉列表中的所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!