【问题标题】:bootstrapSwitch Don't toggle switch on clickbootstrapSwitch 不要在点击时切换开关
【发布时间】:2021-10-04 01:05:50
【问题描述】:

我有一个引导开关,但我不希望它每次都切换。我想检查一个条件是否为真,然后才切换开关。我怎样才能做到这一点?这是我目前所拥有的:

$("[name='relVal']").bootstrapSwitch({
    onSwitchChange: function(event, state) {
       if (condition_is_met) {
          $("[name='relVal']").bootstrapSwitch('state', true);
       }
    }
})

【问题讨论】:

    标签: javascript html twitter-bootstrap bootstrap-switch


    【解决方案1】:

    From the docs:

    onSwitchChange

    在开关状态改变时执行的回调函数。如果返回 false,则状态将被恢复,否则没有任何变化

    因此,您需要做的就是返回 true/false 以便切换是否实际发生。

    $("[name='relVal']").bootstrapSwitch({
        onSwitchChange: function(event, state) {
            if (condition_is_met) {
                return true;
            }
    
            return false;
    
            // Or as a one-liner
            // return condition_is_met;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2020-05-21
      • 2019-11-15
      • 1970-01-01
      • 2010-12-14
      • 1970-01-01
      • 2017-10-01
      • 2016-11-22
      • 1970-01-01
      相关资源
      最近更新 更多