引导选择事件参数

Ale*_*der 5 jquery twitter-bootstrap bootstrap-select

我正在使用引导程序选择插件(http://silviomoreto.github.io/)并且我正在处理更改事件“changed.bs.select”以捕获任何选择更改。根据https://silviomoreto.github.io/bootstrap-select/options/ 上的文档,“此事件在选择的值更改后触发。它通过事件、clickedIndex、newValue、oldValue。”

我想了解 newValue 和 oldValue 究竟代表什么。因为总是出现新值为真而旧值为假的情况。

这是一个小提琴来说明我的观点。

https://jsfiddle.net/muojdbh9/

HTML

<select class="selectpicker">
  <option>Mustard</option>
  <option>Ketchup</option>
  <option>Relish</option>
</select>

<div class="res">
Waiting for a change event
</div>
Run Code Online (Sandbox Code Playgroud)

jQuery

$(".selectpicker").on('changed.bs.select', function (event, clickedIndex, newValue, oldValue) {
    $("div.res").html("newValue: " + newValue + "<BR>oldValue: " + oldValue);
});
Run Code Online (Sandbox Code Playgroud)

Eog*_*dhg 3

这不是一个错误。看起来newValueoldValue没有按照您的预期行事。两者newValueoldValue操作方式相同。您看到的布尔值指的是所选选项的先前状态和新状态。

true代表选中,false代表未选中。

如果新选择的选项之前未处于选定状态,它将像这样返回:

newValue: trueoldValue: false

如果先前选择了所选选项,它将像这样返回:

newValue: false因为oldValue: true它取消了该元素的选择。

caseyjhol 在这里解决了这个问题:

https://github.com/silviomoreto/bootstrap-select/issues/1378/#issuecomment-216393099