bootstrap-switch如何设置初始值

广告:宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取~~~

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>
登录后复制

也可以用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如何设置初始值的详细内容,更多请关注9543建站博客其它相关文章!

9543建站博客
一个专注于网站开发、微信开发的技术类纯净博客。
作者头像
admin创始人

肥猫,知名SEO博客站长,14年SEO经验。

上一篇:总结分享一些小程序开发中遇到的问题(帮忙避坑)
下一篇:浅谈Bootstrap中处理树列表条件和查询条件的方法

发表评论

关闭广告
关闭广告