Javascript 在 bootstrap-select 中获取当前值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41843395/
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 the current value in bootstrap-select
提问by Gene9y
I used e.target.valueto retrieve the current selected value from bootstrap-select but it returned the first selected value i.e. the alert kept displaying item 1, when item 1 and item 2 where selected even when item 2 was the most recently selected.
我曾经e.target.value从 bootstrap-select 中检索当前选定的值,但它返回了第一个选定的值,即警报继续显示项目 1,当项目 1 和项目 2 被选中时,即使项目 2 是最近选择的。
How can I get the value for the recent selection and how can I get how many options are selected?
如何获取最近选择的值以及如何获取选择了多少个选项?
<select multiple class="selectpicker" multiple data-selected-text-format="count > 1">
<option value="item 1">Item 1</option>
<option value="item 2">Item 2</option>
<option value="item 3">Item 3</option>
<option value="item 4">Item 4</option>
<option value="item 5">Item 5</option>
$('.selectpicker').change(function (e) {
alert(e.target.value);
});
回答by Mirza Obaid
I think the answer may be easier to understand like this:
我认为这样的答案可能更容易理解:
$('#empid').on('click',function() {
alert($(this).val());
console.log($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<select id="empid" name="empname" multiple="multiple">
<option value="0">item0</option>
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
<option value="4">item4</option>
</select>
<br />
Hold CTRL / CMD for selecting multiple fields
If you select "item1" and "item2" in the list, the output will be "1,2".
如果在列表中选择“item1”和“item2”,输出将为“1,2”。
回答by kritikaTalwar
instead of e.target.value, you can use $(this).find("option:selected").text();
而不是e.target.value,你可以使用$(this).find("option:selected").text();
$('.selectpicker').change(function () {
var selectedItem = $('.selectpicker').val();
alert(selectedItem);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap-select.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap-select.min.js"></script>
<select class="selectpicker" multiple data-selected-text-format="count > 1">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
<option value="4">Item 4</option>
<option value="5">Item 5</option>
</select>
回答by akshat
$('.selectpicker').on('change', function(){
var selected = []
selected = $('.selectpicker').val()
console.log(selected); //Get the multiple values selected in an array
console.log(selected.length); //Length of the array
});
回答by Jean Dok
回答by Bilal Mustafa
This is the Solution:
这是解决方案:
$('#payment_method').on('hidden.bs.select', function (e) {
// console.log(e.target.value);
console.log(e.target.selectedOptions.length);
$.each( e.target.selectedOptions , function( index, obj ){
console.log(obj.value);
});
});
You get the length of the selections, so may be helpful in for loop
您获得了选择的长度,因此可能对 for 循环有所帮助
console.log(e.target.selectedOptions.length);
and you can loop through the selected values too:
您也可以遍历选定的值:
$.each( e.target.selectedOptions , function( index, obj ){
console.log(obj.value);
});
回答by Sheri Bgh
This is how I get the value of selected Item from select list:
这就是我从选择列表中获取所选项目的值的方式:
var e = document.getElementById("field_ID");
var selected_value = e.options[e.selectedIndex].value;
回答by Icepack
In your case you need to take the value. I use jQuery to help for this:
在您的情况下,您需要取值。我使用 jQuery 来帮助解决这个问题:
$('.selectpicker').change(function (e) {
alert($(e.target).val());
});
回答by Theva
My solution might help somebody
我的解决方案可能会帮助某人
The option value isn't always the same as the text
选项值并不总是与文本相同
$('.selectpicker').on('changed.bs.select', function () {
let val = $(this).siblings('.btn.dropdown-toggle').attr('title');
console.log(val);
});

