如何从 Bootstrap 多个选择器中获取选定值?
How can I get selected values from Bootstrap multiple selectpicker?
这是我的 select
元素,它具有 multiple
属性;
<select id="dd-personnels" onchange="personnelChange()" asp-items="Model.Personnels" multiple data-max-options="1" class="form-control form-control-sm selectpicker"></select>
这是我从上面的 select
初始化 selectpicker
的代码;
$('.selectpicker').selectpicker({ liveSearch: true });
如何在 select 的 onchange
中获取 selected 值?
即使内联事件处理程序是一种不好的做法,您也需要向内联添加两个关键字:
<select id="dd-personnels" onchange="personnelChange(this, event)" ....
现在你的函数是:
function personnelChange(ele, event) {
var val = $(ele).selectpicker('val');
console.log(val);
}
详情请看documentation
片段:
function personnelChange(ele, event) {
var val = $(ele).selectpicker('val');
console.log(val);
}
$('#dd-personnels').selectpicker({
liveSearch: true
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>0
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min.js"></script>
<select id="dd-personnels" onchange="personnelChange(this, event)" asp-items="Model.Personnels" multiple data-max-options="1" class="form-control form-control-sm selectpicker">
<option>Mustard</option>
<option>Ketchup</option>
<option>Barbecue</option>
</select>
这是我的 select
元素,它具有 multiple
属性;
<select id="dd-personnels" onchange="personnelChange()" asp-items="Model.Personnels" multiple data-max-options="1" class="form-control form-control-sm selectpicker"></select>
这是我从上面的 select
初始化 selectpicker
的代码;
$('.selectpicker').selectpicker({ liveSearch: true });
如何在 select 的 onchange
中获取 selected 值?
即使内联事件处理程序是一种不好的做法,您也需要向内联添加两个关键字:
<select id="dd-personnels" onchange="personnelChange(this, event)" ....
现在你的函数是:
function personnelChange(ele, event) {
var val = $(ele).selectpicker('val');
console.log(val);
}
详情请看documentation
片段:
function personnelChange(ele, event) {
var val = $(ele).selectpicker('val');
console.log(val);
}
$('#dd-personnels').selectpicker({
liveSearch: true
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>0
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min.js"></script>
<select id="dd-personnels" onchange="personnelChange(this, event)" asp-items="Model.Personnels" multiple data-max-options="1" class="form-control form-control-sm selectpicker">
<option>Mustard</option>
<option>Ketchup</option>
<option>Barbecue</option>
</select>