Bootstrap-select 使用 jQuery 添加基于 selected 选项的操作

Bootstrap-select add actions based on selected options using jQuery

我有一个 bootstrap-select,我想根据用户交互添加条件。

如果用户打开下拉菜单并关闭用户:

  1. selected 与显示的初始值相比不同的值,它会提醒 'value changed'
  2. 在显示时保持当前值等于初始值,它会提醒 'same value'
  3. 没有select任何选项,它会提醒'no value'

请在下面找到一个带有说明的简单演示。我不明白为什么我的逻辑不能正常工作。

我做的是:

我创建了一个全局变量 initVal 并在 show.bs.select 事件中存储了初始值。

然后在 hide.bs.select 事件上我检查是否有 is/are 选项 selected val.length > 0 并且值与初始值不同 initVal != val然后提醒 'value changed'

然后如果再次有 is/are 选项 selected val.length > 0 并且值等于初始值 initVal == val 然后警告 'same value'

然后如果没有 selected val.length == 0 然后警告 'any value'

要查看问题,select 例如一个国家并关闭下拉列表并再次打开它。它每次都会向您展示新的价值。或者当一个 X 国家被 selected 时,所以 select 其他国家然后取消 select 它们并且在关闭之前保持相同的 X 国家并且它不会显示正确的结果。

有什么建议吗?非常感谢。

$(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");  
}

});


} );

你的HTML原样。