twitter-bootstrap 获取按钮组中的活动按钮(引导程序)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18978590/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 18:34:04  来源:igfitidea点击:

Getting active buttons in button-group (bootstrap)

javascripttwitter-bootstraptwitter-bootstrap-3

提问by stUrb

I have a button group and am willing to update some other field on change according to the active buttons. See the jsfiddle here

我有一个按钮组,并愿意根据活动按钮更新其他一些字段。在这里查看jsfiddle

This is the HTML, copied from the documentation:

这是从文档中复制的 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>

The Button row acts like it should do.
Then I watch for a click event on the .arrUpdatediv for change. I've got multiple button groups which all have the .arrUpdateclass. That's the reason for the second class: .arrActiviteit

按钮行就像它应该做的那样。
然后我观察.arrUpdatediv上的点击事件是否有变化。我有多个按钮组,它们都有.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); 

})

But it appears that the 'active' class gets added afterthe click event is fired. So the value in #hierHieris always behind one click, so to say.

但似乎在触发点击事件添加了“活动”类。所以 in 的值#hierHier总是落后于一键,可以这么说。

How can I resolve this?
Or is there a better way to retrieve all the active buttons in this checkbox-array?

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

采纳答案by rafaelcastrocouto

SOLVED

解决了



update: better solution Twitter Bootstrap onclick event on buttons-radio

更新:更好的解决方案Twitter Bootstrap onclick 事件按钮收音机



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

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

I solved it using a timeout to make your function run after bootstrap events...

我使用超时解决了它,使您的函数在引导事件后运行...

There must be some other ways to do it...

一定有其他方法可以做到这一点......

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