jQuery Twitter Bootstrap:如何查看切换按钮的状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13323671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Twitter Bootstrap: How to see the state of a toggle button?
提问by Houman
How do I see the state of a toggle button?
如何查看切换按钮的状态?
With a checkbox I can listen to "changed" event of the checkbox and do a $(this).is(":checked")
to see what state it has.
使用复选框,我可以收听复选框的“已更改”事件并$(this).is(":checked")
查看它的状态。
<a id="myId" class="btn" data-toggle="button"/>
But not sure how to do that with a toggle button?
但不确定如何使用切换按钮来做到这一点?
回答by Yohn
you can see what classes the button has..
你可以看到按钮有什么类..
$(this).hasClass('disabled') // for disabled states
$(this).hasClass('active') // for active states
$(this).is(':disabled') // for disabled buttons only
the is(':disabled')
works for buttons, but not the link btns
该is(':disabled')
作品的按钮,而不是链接btns
回答by reubano
If you're using jQuery to intercept the clickevent like so...
如果你使用 jQuery 来拦截点击事件,就像这样......
$(this).click(callback)
you need to get creative because .hasClass('active')
doesn't report the correct value. In the callback
function put the following:
您需要发挥创意,因为.hasClass('active')
没有报告正确的值。在callback
函数中输入以下内容:
$(this).toggleClass('checked')
$(this).hasClass('checked')