如何使用jQuery获取Twitter Bootstrap切换开关的值

25

我正在使用 http:// bootstrapswitch.site/ 制作切换开关按钮。如何使用jQuery获取切换开关的“on”或“off”状态?

我的代码是:

<div class="make-switch  switch-small"><input type="radio" class="nsfw"></div>

请告诉我如何解决这个问题。
4个回答

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

请参考此链接以获取更多功能。


2
$('#switcher').bootstrapSwitch('state'); - user2744169
1
为什么 $('#switcher').bootstrapSwitch('state'); 会覆盖现有的小部件? - JaskeyLam
@janhartmann 看起来有一个更新已经完成,所以 $('#switcher').bootstrapSwitch('setState', false); 不再起作用,应该改为 $('#switcher').bootstrapSwitch('state', false); - Dalin Huang
已修复 @DanielH :-) - janhartmann

17
据我所知,该方法已经更新(但文档不太完善)。
$('#switcher').bootstrapSwitch('state', true)
$('#switcher').bootstrapSwitch('state', false)

1
奇怪,文档仍未更新!感谢您的提示! - Anupam
这个对我有效,谢谢!(看起来像是一个更新) - Dalin Huang

3

关于在我的情况下改变状态,

('#switcher').bootstrapSwitch('state', false); 

这是正确的代码。


1
$('#switcher').bootstrapSwitch('state');

对我无效,但在我的版本中:

$("#switcher").bootstrapSwitch("status");

可以正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接