简体   繁体   English

检查一组复选框,是否在Bootstrap中选中了一个复选框。

[英]Checking a set of checkbox if a checkbox is checked in Bootstrap or not.

Hello, I'm having a problem when performing this in bootstrap 3. I have this code working in jsfiddle but it doesn't work bootstrap. 您好,在bootstrap 3中执行此操作时遇到问题。我的代码在jsfiddle中工作,但是在bootstrap中不起作用。

Here's the jsfiddle 这是jsfiddle

HTML HTML

<div class="col-md-3 span6 privilege-container-b">
<label class="checkbox">
    <input type="checkbox" name="mypriv" value="test_priv1" class="test_priv1">Checkbox 1</label>
<label class="checkbox">
    <input type="checkbox" name="mypriv" value="test_priv2" class="test_priv2">Checkbox 2</label>
<label class="checkbox">
    <input type="checkbox" name="mypriv" value="test_priv3" class="test_priv3">Checkbox 3</label>
<label class="checkbox">
    <input type="checkbox" name="mypriv" value="test_priv4" class="test_priv4">Checkbox 4</label>
<label class="checkbox">
    <input type="checkbox" name="mypriv" value="test_priv5" class="test_priv5">Checkbox 5</label>
<label class="checkbox">
    <input type="checkbox" name="mypriv" value="test_priv6" class="test_priv6">Checkbox 6</label>
</div>

Jquery jQuery的

$('.test_priv1').change(function () {
    var val = $('.test_priv1').is(':checked');
    if (val === true) {
        $('.test_priv2, test_priv3').prop('checked', true);
        $('.test_priv2, test_priv3').prop('disabled', true);
    } else {
        $('.test_priv2, test_priv3').prop('checked', false);
        $('.test_priv2, test_priv3').prop('disabled', false);
    }
    return false;
});

The code above doesn't work. 上面的代码不起作用。 My goal is if test_priv1 is checked, test_priv2 and test_priv3 will be checked and locked. 我的目标是如果选中了test_priv1,则将检查并锁定test_priv2和test_priv3。 Any thing wrong with my code? 我的代码有什么问题吗? Please correct me guys. 请纠正我。 Thanks 谢谢

I would add the dot to the text_priv3 selector and do 我将点添加到text_priv3选择器并执行

Live Demo 现场演示

$(function() {
    $('.test_priv1').on("click", function () { // IE<9 triggers change after blurred
        var checked = $(this).is(':checked');
        var $otherChecks = $('.test_priv2, .test_priv3');
        $otherChecks.prop('checked', checked);
        $otherChecks.prop('disabled', checked);
        $otherChecks.parent().toggleClass("disabled",checked);
    });
});

Note I also greyed the label text 注意我也将标签文本设为灰色

If you by the way want to grey out all the other boxes, regardless of class you can do 顺便说一句,如果您想将所有其他框都涂成灰色,则无论班级如何,都可以

var $otherChecks = $("input[name='mypriv']:not('.test_priv1')");

or 要么

var $otherChecks = $("input[name='mypriv']").not(this);

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

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