首先需要引入bootstrap的css和js文件,再引入bootstrap-switch.css和bootstrap-switch.js文件

  1. <script type="text/javascript" src="bootstrap-switch.min.js"></script>
  2. <link rel="stylesheet" type="text/css" href="bootstrap-switch.min.css" />

下载地址:http://www.bootcss.com/p/bootstrap-switch/

html代码:

  1. <div class="switch">
  2. <input type="checkbox" name="switch">
  3. </div>

通过js实现开关的初始化:

  1. $('[name="switch"]').bootstrapSwitch({
  2. onText:"启动",
  3. offText:"停止",
  4. onColor:"success",
  5. offColor:"info",
  6. size:"small",
  7. onSwitchChange:function(event,state){
  8. if(state==true){
  9. $(this).val("1");
  10. }else{
  11. $(this).val("2");
  12. }
  13. }
  14. })

覆盖全局属性:

  1. $.fn.bootstrapSwitch.defaults.size = 'large';
  2. $.fn.bootstrapSwitch.defaults.onColor = 'success';

bootstrap-switch若想根据动态数据控制switch的状态,使用$(this).bootstrapSwitch('state',!state,true);

贴代码

  1. $(element[0]).children().bootstrapSwitch().on('switchChange.bootstrapSwitch',function(target,state){
  2. $(this).bootstrapSwitch('state',!state,true);//阻止switch状态变化
  3.  
  4. //......此处省略动态获取数据的代码
  5.  
  6. //如果TRUE则状态改变,否则状态不变
  7. if(...){
  8. $this.bootstrapSwitch('toggleState',true);
  9. }
  10. })

bootstrap-switch的属性:

bootstrap-switch属性
js属性名 html属性名 类型 描述 取值范围 默认值
state checked Boolean 选中状态 true、false true
size data-size String 开关大小 null、mini、small、normal、large null
animate data-animate Boolean 动画效果 true、false true
disabled disabled Boolean 禁用开关 ture、false false
readonly readonly Boolean 开关状态只读,不能修改 true、false false
indeterminate data-indeterminate Boolean 模态 true、false false
inverse data-inverse Boolean 颠倒开关顺序 true、false false
radioAllOff data-radio-all-off Boolean 允许单选按钮被用户取消选中 true、false false
onColor data-on-color String 左侧开关颜色 primary、info、success、warning、danger、default primary
offColor data-off-color String 右侧开关颜色 primary、info、success、warning、danger、default default
onText data-on-text String 左侧开关显示文本 String ON
offText data-off-text String 右侧开关显示文本 String OFF
labelText data-label-text String 开关中间显示文本 String &nbsp;
handleWidth data-handle-width String|Number 开关左右2侧的宽度 String|Number auto
labelWidth data-label-width String|Number 开关中间的宽度 String|Number auto
baseClass data-base-class String 开关基础样式 String bootstrap-switch
wrapperClass data-wrapper-class String | Array 元素样式容器 String | Array wrapper
onInit   function 初始化开关 Function function(event,state){}
onSwitchChange   function 当开关状态改变时触发 Function function(event,state){}

bootstrap-switch的更多相关文章

  1. bootstrap switch功能

    bootstrap switch是一个按钮开关,点击时获取其状态可通过以下代码: <input id="email_switch_state" type="chec ...

  2. 前端插件之Bootstrap Switch 选择框开关控制

    简介 Bootstrap Switch是一款轻量级插件,可以给选择框设置类似于开关的样式 它是依赖于Bootstrap的一款插件 下载 下载地址 在线引用 导入 因为它是依赖于Bootstrap的一款 ...

  3. bootstrap switch样式修改与多列等间距布局

    先以一张图开启今天的随笔 今天实习遇到了switch按钮,小姐姐说用插件bootstrap switch来写,我第一次用这个插件,首先在引入方面就遇到了很多坑,先来总结一下bootstrap swit ...

  4. 前端基础(十):Bootstrap Switch 选择框开关控制

    简介 Bootstrap Switch是一款轻量级插件,可以给选择框设置类似于开关的样式 它是依赖于Bootstrap的一款插件 下载 下载地址 在线引用 导入 因为它是依赖于Bootstrap的一款 ...

  5. 使用jQuery获取Bootstrap Switch的值

    $('#switcher').bootstrapSwitch('state'); // true || false $('#switcher').bootstrapSwitch('toggleStat ...

  6. bootstrap Switch 的一个坑点

    在bootstrap的modal点开的时候改变bootstrapSwitch的状态的时候,会出现第一次打开modal,switch没有变化,第二次以后打开modal才会改变,这个问题找了好久没有找到答 ...

  7. Bootstrap switch 切换状态踩坑

    Boostrap switch 下载地址(http://www.bootcss.com/p/bootstrap-switch/),同时配有一些简单的用例. 其中写到 Toggle State切换状态的 ...

  8. 基于Bootstrap的jQuery开关按钮插件

        按钮 下载 使用方法 首先要在页面中引入依赖文件: jquery.Bootstrap.Bootstrap Switch CSS和Bootstrap Switch JS.这里用的是bootstr ...

  9. 基于Bootstrap的超酷jQuery开关按钮插件

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJgAAAA2CAIAAAC0tsfoAAAB/0lEQVR4nO3bQWrCQBTGcc/mbVz0DD

  10. Bootstrap相关优质项目推荐

    Bootstrap 编码规范by @mdo Bootstrap 编码规范:编写灵活.稳定.高质量的 HTML 和 CSS 代码的规范. jQuery API 中文手册 根据最新的 jQuery 1.1 ...

随机推荐

  1. Codeforces Round #299 (Div. 2) D. Tavas and Malekas kmp

    题目链接: http://codeforces.com/problemset/problem/535/D D. Tavas and Malekas time limit per test2 secon ...

  2. apache 2.4目录权限

    apache 2.4 好象不再支持以下指令...Order allow,denyAllow from all 用上面的指令访问页面时显示错误:client denied by server confi ...

  3. 由于MDK5.0A没有STM32F103程序错误 stm32f10x.h(298): error: #67: expected a "}"

    转自:http://blog.163.com/lby147612@126/blog/static/17041045220150130438428/ 由于MDK4.72A没有STM32F030,所以升级 ...

  4. Android开发中常见的内存泄露案例以及解决方法总结

    1.单例模式引起的内存泄露 由于单例模式的静态特性,使得它的生命周期和我们的应用一样长,如果让单例无限制的持有Activity的强引用就会导致内存泄漏如错误代码示例: public class Use ...

  5. vue 中ref 的使用注意事项

    最近看别人的项目发现有些语法不能理解,所以百度进行了学习.现在总结一下. ref 有两种用法 1.ref 加在普通的元素上,用this.ref.name 获取到的是dom元素 2.ref 加在子组件上 ...

  6. 关于JEE web项目 Servlet中 “/” 的解释 ;

    1.关于"/" 可以代表web应用的根目录,也可以代表站点的根目录: 1>如果交给浏览器解析,则代表web站点的根目录,如果交给web服务器解析则代表项目的根目录: 2> ...

  7. Linux内核0.11 makefile文件说明

    # # if you want the ram-disk device, define this to be the # size in blocks. # 如果要使用 RAM 就定义块的大小(注释掉 ...

  8. [cnbeta] 波音系列飞机价格。。。

    https://www.cnbeta.com/articles/tech/786745.htm 单价最便宜的是波音737-700,为0.858亿美元(约合5.96亿元). 评论网友调侃,“你家能满40 ...

  9. python3+selenium 牛刀小试

    # coding:utf-8 # __author__ = 'Carry' import unittest from selenium import webdriver import time cla ...

  10. 在java中为什么要把main方法定义为一个static方法?

    我们知道,在C/C++当中,这个main方法并不是属于某一个类的,它是一个全局的方法,所以当我们执行的时候,c++编译器很容易的就能找到这个main方法,然而当我们执行一个java程序的时候,因为ja ...