动态设置bootstrapswitch状态

checkbox的html

<input type="checkbox" name="mySwitch" id="mySwitch">

引用bootstrap-switch的css和js

<link href="~/Content/bootstrap-switsh/bootstrap-switch.css" rel="stylesheet" />
 <script src="~/Content/js/jquery/jquery-2.1.1.min.js"></script>@*jquery有一定的版本要求,版本太低可能会让bootstrapswitch事件无法应用*@
 <script src="~/Content/bootstrap-switsh/bootstrap-switch.js"></script>

  

动态设置某个checkbox开关控制状态  

function load() {  
     //获取控件初始状态window.parent.Warn_Start为false或者true
    var r = window.parent.Warn_Start
    //有则销毁(Destroy)
    $('#mySwitch').bootstrapSwitch('destroy');//定义按钮刷新时可添加,否则可以不加
    //重新加载
    $('#mySwitch').bootstrapSwitch({
            onText: "启动",
            offText: "停止",
            onColor: "success",
            offColor: "info",
            size: "small",
            state: r,
            onSwitchChange: function (event, state) {
                window.parent.Warn_Start = state;
          var $el = $(event.el)
          , value = event.value;
          console.log(event, $el, value);
                if (state == true) {
                    Msg.notifySuccess("已启动");
                } else {
                    Msg.notifySuccess("已关闭");
                }
            }

        })
}

猜你喜欢

转载自www.cnblogs.com/Loners/p/10297990.html