简体   繁体   English

引导开关从不确定状态

[英]Bootstrap Switch from indeterminate state

Starting with this fiddle: 从这个小提琴开始:

https://jsfiddle.net/okuem1fb/ 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'));
});

From the indeterminate state the button reacts based on which side you click: Click on 'On' and the switch goes to 'Off' Click on 'Off' and the switch goes to 'On'. 在不确定状态下,按钮会根据您单击的那一侧做出反应:单击“开”,开关转到“关”。单击“关”,开关转到“开”。

I would like to change that, when I click on the side of 'On' I actually expect it to go to the status 'On' and vice versa for 'Off'... 我想更改一下,当我单击“打开”的一侧时,我实际上希望它变为“打开”状态,反之亦然,“关闭” ...

Also when clicking on 'Off' from the indeterminate state, the event used doesn't trigger... 另外,当从不确定状态单击“关闭”时,使用的事件不会触发...

Any thoughts on how to change this? 关于如何改变这种想法?

I hope that the solution has been found since.... 我希望从那时起找到解决方案。

In case for those who are in the same situation, here is the solution. 对于那些处于相同情况的人,这里是解决方案。 It is necessary to modify the js file (bootstrap-switch.js) and find the line corresponding to : 有必要修改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');
    });
  }

That will modify the switch to go "On" when you click on "On" and go "Off" when clicking on the other-side. 这将修改开关,使其在您单击“开”时变为“开”,而在另一侧单击时变为“关”。 You can modify the minified file the same way (juste have to find which specific places to modify)... 您可以用相同的方式修改缩小的文件(只是必须找到要修改的特定位置)...

For the change event, after initialization, you have to set the switch state to null : 对于更改事件,在初始化之后,必须将开关状态设置为null:

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

This has been discused on Github here : https://github.com/Bttstrp/bootstrap-switch/issues/426 (with a corresponding JSFiddle : http://jsfiddle.net/2m4b4bb2/3/ ) 这已经在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