bootstrap - switch的初始化和监听事件

 一、Bootstrap Switch 选择框开关控制 

依赖于Bootstrap的一款轻量级插件,可以给选择框设置类似于开关的样式

 

二、使用时注意初始化:

js初始化:

  $('[class="form-control switch-open"]').bootstrapSwitch({
                size: 'small',
                onSwitchChange: function (event, state) {
                    var ProductId = event.target.defaultValue;

                    if (state == true) {
                        //  updateStatus(ProductId, '1');   
                         console.log(1)
                    } else {
                        console.log(0)
                    }

                }
            });

 

 三、使用步骤

1首先引入jquery

2然后引入bootstrap的js和css

3引入bootstrap的switch的js和css

<link href="bootstrap.cssrel="stylesheet" type="text/css">

<link href="bootstrap-switch/bootstrap-switch.min.cssrel="stylesheet" type="text/css" >

<script type="text/javascript" src="js/jquery.min.js"></script>

<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/plugins/bootstrap-switch/bootstrap-switch.min.js"></script>

4.对页面上指定的input【type="checkbox"】

如1:

<input type="checkbox" name="my-switch" checked>

  js初始化:(  $(document).ready(function () {...  })(否则无效)

$("[name='my-switch']").bootstrapSwitch();

或者:

  // 初始化 注意:state 属性的设置一定放在最后
 $("[name='my-switch']").bootstrapSwitch('onText','开启').bootstrapSwitch('offText','关闭').bootstrapSwitch("onColor",'success') .bootstrapSwitch('state',true);

 $("[name='my-switch']").bootstrapSwitch("onSwitchChange",function(event,state){
    var val='';
    var text='';
    if(state==true){
        val=1;
        text='开启';
    }else{
        val=0;
        text='关闭';
    }
})

 

如2:

 <input  type="checkbox"  class='form-control switch-open' name='switch-open'>

  js初始化:(  $(document).ready(function () {...  })(否则无效)

for (var i = 0 ; i < $('[class="form-control switch-open"]').length ; i++)
            {
                var obj = $('[class="form-control switch-open"]').eq(i);
                if (obj.is(":checked")) {
                    CollectChannelSet($(obj) , true)
                } else {
                    CollectChannelSet($(obj) , false)
                }
                
            }
$(
'[class="form-control switch-open"]').bootstrapSwitch({ onText:"打开",  
            offText:"关闭",  
            onColor:"success",  
            offColor:"primary",  
            size:"small",      
   
onSwitchChange: function ( event, state) { if ($(this).is(":checked")) { CollectChannelSet($(this).parents("td").index(), true) } else { CollectChannelSet($(this).parents("td").index(), false) } } });

 参考: 前端插件之Bootstrap Switch 选择框开关控制   https://www.cnblogs.com/fu-yong/p/8794457.html

转载于:https://www.cnblogs.com/July-/p/9566559.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值