[英]How to get selected values in SumoSelect dropdown?
I am using the SumoSelect dropdown for multiselect options. 我正在使用SumoSelect下拉列表来选择多选项。 But i cannot get the selected values array.
但我无法获得所选的值数组。 Below the sample code :
下面的示例代码:
<script type="text/javascript">
$(document).ready(function () {
window.testSelAll = $('.testSelAll').SumoSelect({okCancelInMulti:true, selectAll:true });
$('.btnOk').on('click', function(){
var obj = [];
$('option:selected').each(function () {
obj.push($(this).index());
alert("Selected Values=="+$(this).val());
});
for (var i = 0; i < obj.length; i++) {
$('.testSelAll')[0].sumo.unSelectItem(obj[i]);
}
});
});
</script>
<select multiple="multiple" placeholder="Share Your Friends" onchange="console.log($(this).children(':selected').length)" class="testSelAll">
<option value="1">Volvo</option>
<option value="2">Saab</option>
<option value="3">Mercedes</option>
<option value="audi">Audi</option>
<option value="bmw">BMW</option>
<option value="porsche">Porche</option>
<option value="ferrari">Ferrari</option>
<option value="mitsubishi">Mitsubishi</option>
</select>
If you want the selected values instead of the text , just change .text()
to .val()
. 如果您想要选择的值而不是文本 ,只需将
.text()
更改为.val()
。
If you want to get the array, see below with working example at the bottom. 如果你想获得数组,请参阅下面的底部的工作示例。
jQuery jQuery的
$(document).ready(function() {
$('.testSelAll').SumoSelect({
okCancelInMulti: true,
selectAll: true
});
$('.btnOk').on('click', function() {
var obj = [],
items = '';
$('.testSelAll option:selected').each(function(i) {
obj.push($(this).val());
$('.testSelAll')[0].sumo.unSelectItem(i);
});
for (var i = 0; i < obj.length; i++) {
items += ' ' + obj[i]
};
alert(items);
});
});
HTML HTML
<select multiple="multiple" class="testSelAll">
<option value="car1">Volvo</option>
<option value="car2">Saab</option>
<option value="car3">Mercedes</option>
<option value="car4">Audi</option>
</select>
You can get them from underlying hidden select element. 您可以从底层隐藏的选择元素中获取它们。 using jquery eg.
使用jquery例如。
$('.select1 option:selected')
I think the cleanest way to do this. 我认为这是最干净的方法。 Is to take advantage of html5 select element underlying SumoSelect.
是利用基于SumoSelect的html5选择元素。
HTML HTML
<select multiple="multiple" class="testSelAll" id="multi-select">
<option value="car1">Volvo</option>
<option value="car2">Saab</option>
<option value="car3">Mercedes</option>
<option value="car4">Audi</option>
</select>
Javascript 使用Javascript
var values = $('#multi-select').val();
This line will return a string list of the values selected. 该行将返回所选值的字符串列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.