简体   繁体   English

在Twitter Bootstrap上切换复选框

[英]Toggle checkbox on Twitter Bootstrap

I made a group of checkboxes with Twitter Bootstrap. 我用Twitter Bootstrap制作了一组复选框。 Now I would like to do a "select all" or "deselect all" button. 现在我想做一个“全选”或“取消全选”按钮。 How could I do this using a JS function? 我怎么能用JS函数做到这一点?

The Twitter Bootstrap documentation says to use $().button('toggle') but it doesn't work. Twitter Bootstrap文档说使用$()。button('toggle')但它不起作用。

Here is my code snippet: 这是我的代码片段:

<div class="control-group">
<div class="controls">
    <div class="input">
        <div class="btn-group">
            <label class="btn  btn-toggle" for="colors-1-toggle-0">
                <input type="checkbox" id="colors-1-toggle-0" name="colors-1[]" value="color_c" />C
            </label>
            <label class="btn  btn-toggle" for="colors-1-toggle-1">
                <input type="checkbox" id="colors-1-toggle-1" name="colors-1[]" value="color_m" />M
            </label>
            <label class="btn  btn-toggle" for="colors-1-toggle-2">
                <input type="checkbox" id="colors-1-toggle-2" name="colors-1[]" value="color_y" />Y
            </label>
            <label class="btn  btn-toggle" for="colors-1-toggle-3">
                <input type="checkbox" id="colors-1-toggle-3" name="colors-1[]" value="color_k" />K
            </label>
        </div>
    </div>
</div></div>

Technically, the Twitter Bootstrap Buttons plugin is meant to work with <button> elements and have them behave as though they were radio or checkbox inputs. 从技术上讲,Twitter Bootstrap Buttons插件可以与<button>元素一起使用,并使它们表现得好像是无线电或复选框输入。

Also, $().button('toggle') is just psuedo code, and in practice is meant to be used with a selector, eg, $('.btn-toggle').button('toggle') . 此外, $().button('toggle')只是伪代码,实际上是与选择器一起使用,例如$('.btn-toggle').button('toggle') However, your example is a bit substandard, since you have assigned classes intended for buttons to <label> elements. 但是,您的示例有点不合标准,因为您已为<label>元素分配了用于按钮的类。

Anyway, despite all that, if all you want to do is set all your input boxes to true, you could use something like: 无论如何,尽管如此,如果您想要做的就是将所有输入框设置为true,您可以使用以下内容:

$('.btn-group input[type="checkbox"]').prop('checked', true);

JSFiddle 的jsfiddle

If you wanted to do it with <button> elements, it would be something like this: 如果你想用<button>元素来做,它将是这样的:

JSFiddle 的jsfiddle

However, that drops a bunch of the input data which you're working with. 但是,这会丢弃您正在使用的一堆输入数据。

Thank you merv! 谢谢merv! Thanks to you I came to this solution: 谢谢你,我来到了这个解决方案:

<button id="toggle-colors" class="btn btn-info">Toggle</button>
<script>
    $('#toggle-colors').click(function() {
        $('.btn-group input[name^="colors"]').each(function(){
            // toggle checkbox
            $(this).prop('checked',!$(this).prop('checked'));
            // toggle class
            $(this).parents('label').toggleClass('active');
        });
    });
</script>

I don't use the button tag as Twitter Bootstrap recommends because it doesn't send the value per POST. 我没有像Twitter Bootstrap推荐的那样使用按钮标签,因为它不会按POST发送值。

I found a way to do this without JS 没有JS,我找到了一种方法

http://codepen.io/ibanez182/pen/WxKABq/ http://codepen.io/ibanez182/pen/WxKABq/

<div class="form-group">
    <div class="checkbox">
        <label data-toggle="collapse" data-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
            <input type="checkbox"/> A checkbox
        </label>
    </div>
</div>
<div id="collapseOne" aria-expanded="false" class="collapse">
    <div class="well">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Saepe ut molestias eius, nam neque esse eos modi corrupti harum fugit, hic recusandae praesentium, minima ipsa eligendi architecto at! Culpa, explicabo.</div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM