bootstrap的switch监听事件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/flting1017/article/details/77409516

今天运用了bootstrap的switch的控件,然后我想当点击switch的时候,会有一些dom的操作,但是我看了官方文档,里面的js拿过来使用的时候,是没有任何效果的

我的switch写法:

< div class = "checkbox checkbox-switch" >
< label >
< input id = "mySwitch" type = "checkbox" data-on-text = "开启" data-off-text = "关闭" class = "switch" data-size = "mini" name = "status" >
</ label >
</ div >

bootstrap的官方文档事件处理内容:

事件处理(Event handler) javascript

ON OFF
$('#mySwitch').on('switch-change', function (e, data) {
    var $el = $(data.el)
      , value = data.value;
    console.log(e, $el, value);
});
上面这个对于我的switch无效,我后面查了一些文档和别人写的一些博客,然后这个方法对于我的switch有效果:(可以正常打印)

$( 'input[type=checkbox]').bootstrapSwitch({ onSwitchChange: function(){
console. log( 111)
} });

参考的bootstrap switch文章链接:

http://www.jb51.net/article/88463.htm



补充一下怎么建立switch

首先引入jquery

然后引入bootstrap

最后引入bootstrap的switch的js和css

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

<link href="bootstrap-switch/bootstrap-switch.min.css" rel="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>

然后给个input

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

js写以下代码,不然switch控件出不来

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




猜你喜欢

转载自blog.csdn.net/flting1017/article/details/77409516