簡體   English   中英

引導開關從不確定狀態

[英]Bootstrap Switch from indeterminate state

從這個小提琴開始:

https://jsfiddle.net/okuem1fb/

<div class="make-switch switch-small">
  <input type="checkbox" checked="true" data-checkbox="VALUE1" class="alert-status">
</div>
<div class="make-switch switch-small">
  <input type="checkbox" checked="true" data-checkbox="VALUE2" class="alert-status pt2">
</div>
<div class="make-switch switch-small">
  <input type="checkbox" checked="true" data-checkbox="VALUE3" class="alert-status">
</div>
<div class="make-switch switch-small">
  <input type="checkbox" checked="true" data-checkbox="VALUE4" class="alert-status">
</div>

$('.alert-status').bootstrapSwitch('state', true);
$(".alert-status pt2").attr('data-indeterminate', 'true');
$('.alert-status').on('switchChange.bootstrapSwitch', function (event, state) {
   alert($(this).data('checkbox'));
});

在不確定狀態下,按鈕會根據您單擊的那一側做出反應:單擊“開”,開關轉到“關”。單擊“關”,開關轉到“開”。

我想更改一下,當我單擊“打開”的一側時,我實際上希望它變為“打開”狀態,反之亦然,“關閉” ...

另外,當從不確定狀態單擊“關閉”時,使用的事件不會觸發...

關於如何改變這種想法?

我希望從那時起找到解決方案。

對於那些處於相同情況的人,這里是解決方案。 有必要修改js文件(bootstrap-switch.js)並找到與以下內容相對應的行:

key: '_handleHandlers',
  value: function _handleHandlers() {
    var _this6 = this;

    this.$on.on('click.bootstrapSwitch', function (event) {
      event.preventDefault();
      event.stopPropagation();
      _this6.state(true); //--> _this6.state(false);
      return _this6.$element.trigger('focus.bootstrapSwitch');
    });
    return this.$off.on('click.bootstrapSwitch', function (event) {
      event.preventDefault();
      event.stopPropagation();
      _this6.state(false); //--> _this6.state(true);
      return _this6.$element.trigger('focus.bootstrapSwitch');
    });
  }

這將修改開關,使其在您單擊“開”時變為“開”,而在另一側單擊時變為“關”。 您可以用相同的方式修改縮小的文件(只是必須找到要修改的特定位置)...

對於更改事件,在初始化之后,必須將開關狀態設置為null:

$("#my-switch").bootstrapSwitch({
    indeterminate: true,
    state: null
});

這已經在Github上討論過了: https : //github.com/Bttstrp/bootstrap-switch/issues/426 (帶有相應的JSFiddle: http : //jsfiddle.net/2m4b4bb2/3/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM