通过 jquery-select2 从多值选择框中获取选定的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12889309/
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
Get Selected value from Multi-Value Select Boxes by jquery-select2?
提问by Kaps Hasija
I am using Select2 Jquery to bind my dropdown which is used for multiple selection . I am using select2 jquery.
我正在使用 Select2 Jquery 来绑定用于多选的下拉列表。我正在使用 select2 jquery。
It's working fine, I can bind my dropdown but I need to get the selected value from my multi-value selector. I am looking for method to get value which is supported by select2 Jquery. it might be having a function get selected value.
my drop down binding code
它工作正常,我可以绑定我的下拉列表,但我需要从我的多值选择器中获取选定的值。我正在寻找获取 select2 Jquery 支持的值的方法。它可能有一个函数获取选定的值。
我的下拉绑定代码
$(".leaderMultiSelctdropdown").select2( {
maximumSelectionSize: 4
});
回答by Manuel Schweigert
alert("Selected value is: "+$(".leaderMultiSelctdropdown").select2("val"));
alternatively, if you used a regular selectbox as base, you should be able to use the normal jquery call, too:
或者,如果您使用常规选择框作为基础,您也应该能够使用普通的 jquery 调用:
alert("Selected value is: "+$(".leaderMultiSelctdropdown").val());
both return an array of the selected keys.
两者都返回所选键的数组。
回答by ksg
I know its late but I think you can try like this
我知道已经晚了,但我认为你可以这样尝试
$("#multipledpdwn").on("select2:select select2:unselect", function (e) {
//this returns all the selected item
var items= $(this).val();
//Gets the last selected item
var lastSelectedItem = e.params.data.id;
})
Hope it may help some one in future.
希望它可以帮助将来的某个人。
回答by Jaskey
Returns the selected data in structure of object:
返回对象结构中的选定数据:
console.log($(".leaderMultiSelctdropdown").select2('data'));
Something like:
就像是:
[{id:"1",text:"Text",disabled:false,selected:true},{id:"2",text:"Text2",disabled:false,selected:true}]
Returns the selected val:
返回选定的 val:
console.log($('.leaderMultiSelctdropdown').val());
console.log($('.leaderMultiSelctdropdown').select2("val"));
Something like:
就像是:
["1", "2"]
回答by user4310702
Try like this,
试试这样,
jQuery('.leaderMultiSelctdropdown').select2('data');
回答by Amro Mustafa
Simply :
简单地 :
$(".leaderMultiSelctdropdown").val()
回答by Deepak Jha
You should try this code.
你应该试试这个代码。
$("#multiple_Package_Ids_checkboxes").on('change', function (e) {
var totAmt = 0;
$.each($(this).find(":selected"), function (i, item) {
totAmt += $(item).data("price");
});
$("#PackTotAmt").text(totAmt);
});
回答by Rodolpho Gheleri
Try this:
尝试这个:
$('.select').on('select2:selecting select2:unselecting', function(e) {
var value = e.params.args.data.id;
});
回答by Hammad Ali
This will get selected value from multi-value select boxes: $("#id option:selected").val()
这将从多值选择框中获得选定的值: $("#id option:selected").val()