[英]Bootstrap-select add actions based on selected options using jQuery
I have a bootstrap-select and I want to add conditions based on user interaction.我有一个引导选择,我想根据用户交互添加条件。
If user opens the dropdown and on close the user:如果用户打开下拉菜单并关闭用户:
Please find below a simple demo with an explanation.请在下面找到一个带有解释的简单演示。 I couldn't figure out it why my logic is not working properly.
我无法弄清楚为什么我的逻辑无法正常工作。
What I did is:我所做的是:
I created a global variable initVal
and on show.bs.select
event I store the initial value.我创建了一个全局变量
initVal
并在show.bs.select
事件上存储了初始值。
Then on hide.bs.select
event I check if there is/are option(s) selected val.length > 0
and the value is different to initial value initVal != val
then alert 'value changed'然后在
hide.bs.select
事件上,我检查是否有/是否选择了选项val.length > 0
并且该值与初始值initVal != val
然后警报“值已更改”
Then if again if there is/are option(s) selected val.length > 0
and the value is equal to initial value initVal == val
then alert 'same value'然后,如果再次选择了选项
val.length > 0
并且该值等于初始值initVal == val
然后警报“相同值”
Then if nothing selected val.length == 0
then alert 'any value'然后如果没有选择
val.length == 0
则警告“任何值”
To see the issue, select for example a country and close the dropdown and open it again.要查看问题,以 select 为例,关闭下拉菜单并再次打开它。 it will show you new value each time.
它每次都会向您显示新的价值。 or when a X country is selected, so select other countries then unselect them and before closing keep the same X country and it will not show correct result.
或者选择X国家时,因此select其他国家然后取消选择,然后关闭之前,请保留相同的X国家,并且不会显示正确的结果。
Any suggestions please?请问有什么建议吗? Thank you very much.
非常感谢。
$(document).ready(function() { $("select").selectpicker(); // declare global variable to store the initial value of my select var initVal; // when the select dropdown opens, store the current value in initVal $("select").on('show.bs.select', function () { initVal = $(this).val(); console.log('initial value is ' + initVal); }); // when the select dropdown is closed, store the current value in val $("select").on("hide.bs.select", function() { var val = $(this).val(); console.log('current value is ' + val); // if the select has at least an option selected AND the current value is different to initial value then show alert new value if ( (val.length > 0) && (initVal;= val) ) { alert("you selected a new value"). } //if the select has at least an option selected AND the current value is equal to initial value then show alert same value else if ( (val;length > 0) && (initVal == val) ) { alert("you selected the same value"). } //if the select hasn't any option selected AND the current value is different to initial value then show alert same value else if ( val;length == 0 ) { alert("no value selected"); } }); } );
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.17/css/bootstrap-select.min.css"> <script src="https://code.jquery.com/jquery-3.5.1.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.17/js/bootstrap-select.min.js"></script> <select class="form-control show-tick" data-container="body" data-header="Select option(s)" data-actions-box="true" data-live-search="true" title="All" data-selected-text-format="count > 0" multiple> <option>Austria</option> <option>Denmark</option> <option>France</option> <option>Japan</option> </select>
$(document).ready(function() {
$("select").selectpicker();
// declare global variable to store the initial value of my select
var initVal;
// when the select dropdown opens, store the current value in initVal
$("select").on('show.bs.select', function () {
initVal = $(this).val().toString();
console.log('initial value is ' + initVal);
});
// when the select dropdown is closed, store the current value in val
$("select").on("hide.bs.select", function() {
var val = $(this).val().toString();
console.log('current value is ' + val);
// if the select has at least an option selected AND the current value is different to initial value then show alert new value
if ( initVal !== val ) {
console.log("new");
//alert("you selected a new value");
}
//if the select has at least an option selected AND the current value is equal to initial value then show alert same value
else if ( initVal === val) {
console.log("same")
//alert("you selected the same value");
}
//if the select hasn't any option selected AND the current value is different to initial value then show alert same value
else if ( val.length == 0 ) {
console.log("none")
//alert("no value selected");
}
});
} );
Your HTML as it is.您的 HTML 原样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.