按钮 - 收音机上的Twitter Bootstrap onclick事件 [英] Twitter Bootstrap onclick event on buttons-radio
问题描述
我有一个Twitter Bootstrap按钮 - 收音机并挂了一个onclick事件。但是我如何检查触发了哪个按钮?
I have a Twitter Bootstrap buttons-radio and hook an onclick event to it. But how do I check which of the buttons that got triggered?
我的第一个想法是简单地检查班级活跃,但这应该会产生竞争条件(结果取决于是否首先处理Twitter Bootstrap事件或我自己的onclick事件。
My first thought was to simply check for the class 'active', but this should create a race condition (result depends on whether the Twitter Bootstrap event or my own onclick event is handled first).
推荐答案
这是一个非常烦人的事情。我最终使用的是:
This is a really annoying one. What I ended up using is this:
首先,创建一组没有数据切换
属性的简单按钮。
First, create a group of simple buttons with no data-toggle
attribute.
<div id="selector" class="btn-group">
<button type="button" class="btn active">Day</button>
<button type="button" class="btn">Week</button>
<button type="button" class="btn">Month</button>
<button type="button" class="btn">Year</button>
</div>
接下来,编写一个事件处理程序,通过激活单击的按钮效果来模拟单选按钮效果停用'所有其他按钮。 (编辑:综合尼克的评论清洁版。)
Next, write an event handler that simulates the radio button effect by 'activating' the clicked one and 'deactivating' all other buttons. ( Integrated Nick's cleaner version from the comments.)
$('#selector button').click(function() {
$(this).addClass('active').siblings().removeClass('active');
// TODO: insert whatever you want to do with $(this) here
});
这篇关于按钮 - 收音机上的Twitter Bootstrap onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!