0

从这个小提琴开始:

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'));
});

在不确定状态下,按钮会根据您单击的哪一侧做出反应: 单击“开”,开关变为“关” 单击“关”,开关变为“开”。

我想改变这一点,当我点击“开”的一侧时,我实际上希望它进入“开”状态,反之亦然“关”......

此外,当从不确定状态单击“关闭”时,使用的事件不会触发......

关于如何改变这一点的任何想法?

4

1 回答 1

0

我希望从那以后找到解决方案....

对于那些处于相同情况的人,这是解决方案。需要修改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/

于 2018-09-18T12:34:04.283 回答