在 Twitter Bootstrap 上切换复选框 [英] Toggle checkbox on Twitter Bootstrap

查看:27
本文介绍了在 Twitter Bootstrap 上切换复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Twitter Bootstrap 创建了一组复选框.现在我想做一个全选"或取消全选"按钮.我怎么能用 JS 函数做到这一点?

Twitter Bootstrap 文档说要使用 $().button('toggle') 但它不起作用.

这是我的代码片段:

<div class="控件"><div class="输入"><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 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 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 class="btn btn-toggle" for="colors-1-toggle-3"><input type="checkbox" id="colors-1-toggle-3" name="colors-1[]" value="color_k"/>K

</div></div>

解决方案

谢谢merv!感谢您,我找到了这个解决方案:

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

我不使用 Twitter Bootstrap 推荐的按钮标签,因为它不会发送每个 POST 的值.

I made a group of checkboxes with Twitter Bootstrap. Now I would like to do a "select all" or "deselect all" button. How could I do this using a JS function?

The Twitter Bootstrap documentation says to use $().button('toggle') but it doesn't work.

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>

解决方案

Thank you 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 上切换复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆