首页 > web前端 > Bootstrap教程 > bootstrap-switch如何设置初始值

bootstrap-switch如何设置初始值

angryTom
发布: 2020-05-15 09:22:36
原创
5118 人浏览过

bootstrap-switch如何设置初始值

 bootstrapSwitch是一个很美观的切换控件,官网上对它的介绍也很详细。下面通过几个demo快速上手bootstrapSwitch。

如果你想了解更多关于bootstrap的知识,可以点击:bootstrap教程

首先,引用相应的js和css文件,把checkbox放进class为“switch”的div中,再在js中初始化

1

$('#mySwitch input').bootstrapSwitch();

登录后复制

就可以使用bootstrapSwitch了。

其中input有两个属性 data-on-text data-off-text,分别为切换时显示的文字,这里设置为YES和NO。代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

   <meta charset="UTF-8">

   <title>this is a bootstrap-switch test</title>

   <script src="jquery-2.2.4.js"></script>

   <script src="bootstrap.js"></script>

   <script src="bootstrap-switch.js"></script>

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

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

 

   <script type="text/javascript">

       $(function(){

           $(&#39;#mySwitch input&#39;).bootstrapSwitch();

       })

   </script>

 

 

<div id="mySwitch">

<input type="checkbox" checked="" data-on-text="YES" data-off-text="NO">

</div>

登录后复制

boot11.png1479463253_2223.png

也可以用js设置这两个属性,选中input元素后,通过方法bootstrapSwitch({attribute:value}) 修改属性。代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>this is a bootstrap-switch test</title>

    <script src="jquery-2.2.4.js"></script>

    <script src="bootstrap.js"></script>

    <script src="bootstrap-switch.js"></script>

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

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

    <script type="text/javascript">

        $(function(){

            $(&#39;#mySwitch input&#39;).bootstrapSwitch({

                onText:&#39;On&#39;,

                offText:&#39;Off&#39;

            });

        })

    </script>

</head>

<body>

<br>

<div  id="mySwitch">

    <input type="checkbox" checked  data-on-text="YES" data-off-text="NO"/>

</div>

</body>

登录后复制

boot12.png1479463089_7146.png

以上是bootstrap-switch如何设置初始值的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板