繁体   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