2013-09-24 53 views
4

我有一个按钮组,并且愿意根据活动按钮更改其他一些更改字段。 见jsfiddle here获取按钮组中的活动按钮(引导程序)

这是HTML,从文档复制:

<div class="btn-group arrActiviteit arrUpdate" data-toggle="buttons"> 
    <label class="btn btn-primary active" data-wat='foo'> 
     <input type="checkbox"> Item 1 
    </label> 
    <label class="btn btn-primary" data-wat='bar'> 
     <input type="checkbox"> Item 2 
    </label> 
    <label class="btn btn-primary" data-wat='something'> 
     <input type="checkbox"> item 3 
    </label> 
    <label class="btn btn-primary" data-wat='orElse'> 
     <input type="checkbox"> item 4 
    </label> 
</div> 

按钮行的行为像它应该做的。
然后我注意.arrUpdate div上的点击事件以进行更改。我有多个按钮组,全部都有.arrUpdate类。这是第二类的原因:.arrActiviteit

$('.arrUpdate').click(function(e){ 
    val = ''; // for holding the temporary values 

    $('.arrActiviteit label').each(function(key, value){ 
     if(value.className.indexOf('active') >=0){ 
      val += value.dataset.wat 
     } 
    }) 

    // just for debug reasons. 
    $('#hierHier').html(val); 

}) 

但看来,“主动”类被添加后点击事件。所以#hierHier的值总是落在一次点击之后,可以这么说。

我该如何解决这个问题?
还是有更好的方法来检索此复选框数组中的所有活动按钮?

+0

把你的代码在这里http://jsfiddle.net/hA423/2/,以便我们可以找出问题。 –

+0

我更新了jsfiddle – stUrb

回答

2

解决


更新:更好的解决方案Twitter Bootstrap onclick event on buttons-radio


http://jsfiddle.net/hA423/7/

我解决它使用超时,使后引导事件的功能运行...

必须有我其他的方式来做到这一点...

$('.btn').on('click',function(e){ 
    setTimeout(count); 
}) 
+1

请参阅此主题以获取更好的解决方案:http://stackoverflow.com/questions/9262827/twitter-bootstrap-onclick-event-on-buttons-radio –

+0

但您认为哪个答案是更好的?那里有很多选择! –

+0

去为另一个...相信我 – rafaelcastrocouto