bootstrap 的 switch 按钮初始化问题

问题描述 :

  不管使用标签 input的 checkbox 生成switch按钮还是使用 js 的 bootstrapSwitch 函数 , 初始化按钮状态始终是关闭的

  (这里我使用的是 bootstrap-switch.js 版本 v3.3.4)

解决方案 :

  跟踪一下源码 , 下面第七行的 prvcontainerPosition.call(_this3); 这句话出现了问题 (源码中大概 162 行)

 1   function prvinit() {
 2     var _this3 = this;
 3 
 4     var init = function init() {
 5       _this3.setPrevOptions();
 6       prvwidth.call(_this3);
 7       prvcontainerPosition.call(_this3);
 8       //... ... 9   }

该方法 prvcontainerPosition 中的 state 获取结果为 undefined

function prvcontainerPosition() {
    var _this2 = this;
    var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.ope;
    //... ...
  }

 修改代码如下 , 传递参数 state 

  function prvinit() {
    var _this3 = this;

    var init = function init() {
      _this3.setPrevOptions();
      prvwidth.call(_this3);
      var state = _this3.prevOptions.state ;
      prvcontainerPosition.call(_this3,state);
     // ... ...
  }

猜你喜欢

转载自www.cnblogs.com/hi-gdl/p/9914384.html