9

我正在为 Twitter Bootstrap 使用 Bootstrap Switch。在这里,我有问题根据选中或未选中的全局复选框选中或取消选中所有复选框

这是引导开关链接http://www.bootstrap-switch.org/

这是我所做的

<!-- this is the global checkbox -->
<div class="make-switch switch-mini">
    <input type="checkbox" id="rowSelectAll">
</div>

<!-- row checkboxs -->
<div class="make-switch switch-mini">
    <input type="checkbox" class="row-select">
</div>
<div class="make-switch switch-mini">
    <input type="checkbox" class="row-select">
</div>
<div class="make-switch switch-mini">
    <input type="checkbox" class="row-select">
</div>

jQuery:

$("#rowSelectAll").click(function () {
    $(".row-select").prop('checked', $(this).prop('checked'));
});

编辑:只尝试了一个全局复选框和一个本地复选框

<div class="make-switch switch-mini">
    <input type="checkbox" id="rowSelectAll">
</div>

<div id="toggle-state-switch" class="make-switch switch-mini row-select">
    <input type="checkbox" class="">
</div>
4

3 回答 3

7

试试这个,应该可以的。另请参阅此 jsfiddle ... http://jsfiddle.net/URAcy/5/

  <!-- this is the global checkbox -->
<div class="make-switch switch-mini" id="rowSelectAll">
    <input type="checkbox" id="rowSelectAllc">
</div>

<!-- row checkboxs -->
<div class="make-switch switch-mini toggle-state-switch">
    <input type="checkbox" class="row-select">
</div>
<div class="make-switch switch-mini toggle-state-switch">
    <input type="checkbox" class="row-select">
</div>
<div class="make-switch switch-mini toggle-state-switch">
    <input type="checkbox" class="row-select">
</div>

JS:

$('#rowSelectAll').on('switch-change', function (e, data) {
    var $el = $(data.el)
      , value = data.value;
        $('.toggle-state-switch').each(function( index ) {
      $(this).bootstrapSwitch('setState' , value);
    });
});
于 2013-09-20T08:00:12.160 回答
2

试试这个:

 <!-- this is the global checkbox -->
<div class="make-switch switch-mini">
    <input type="checkbox" id="rowSelectAll">
</div>

<!-- row checkboxs -->
<div class="myswitch make-switch switch-mini">
    <input type="checkbox" class="row-select">
</div>
<div class="myswitch make-switch switch-mini">
    <input type="checkbox" class="row-select">
</div>
<div class="myswitch make-switch switch-mini">
    <input type="checkbox" class="row-select">
</div>

然后在你的脚本中

    $('#rowSelectAll').on('change', function(){
    $('.myswitch').each(function(){

        if($(this).hasClass('switch-on')){
            $(this).bootstrapSwitch('setState' , false);
            $(this).removeClass('switch-on');
        }else{

       $(this).bootstrapSwitch('setState' , true);
        $(this).addClass('switch-on');
        }
    });
});
于 2013-09-20T07:29:12.787 回答
1

更新了你的小提琴 - [ http://jsfiddle.net/URAcy/6/ ]

它应该是

$('#rowSelectAll').on('switch-change', function(){....}
于 2013-10-25T09:35:34.583 回答