bootstrap-switch如何设置初始值

发布时间:2022-05-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了bootstrap-switch如何设置初始值脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

bootstrap-switch如何设置初始值

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

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

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

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

就可以使用bootstrapSwitch了。

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

<!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(){
            $('#mySwitch input').bootstrapSwitch();
        })
    </script>
</head>
<body>
 <div id="mySwitch">
 <input type="checkbox" checked data-on-text="YES" data-off-text="NO"/>
 </div>
</body>
</html>

bootstrap-switch如何设置初始值

bootstrap-switch如何设置初始值

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

<!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(){
            $('#mySwitch input').bootstrapSwitch({
                onText:'On',
                offText:'Off'
            });
        })
    </script>
</head>
<body>
<br>
<div  id="mySwitch">
    <input type="checkbox" checked  data-on-text="YES" data-off-text="NO"/>
</div>
</body>

bootstrap-switch如何设置初始值

bootstrap-switch如何设置初始值

以上就是bootstrap-switch如何设置初始值的详细内容,更多请关注脚本宝典其它相关文章

脚本宝典总结

以上是脚本宝典为你收集整理的bootstrap-switch如何设置初始值全部内容,希望文章能够帮你解决bootstrap-switch如何设置初始值所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。