gpt4 book ai didi

javascript - 检查一组复选框是否在 Bootstrap 中选中。

转载 作者:行者123 更新时间:2023-12-03 12:31:58 24 4
gpt4 key购买 nike

您好,我在 bootstrap 3 中执行此操作时遇到问题。我有此代码在 jsfiddle 中工作,但它无法在 bootstrap 中工作。

这是jsfiddle

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

$('.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;
});

上面的代码不起作用。我的目标是,如果检查 test_priv1,则将检查并锁定 test_priv2 和 test_priv3。我的代码有什么问题吗?请大家指正。谢谢

最佳答案

我会将点添加到 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);
});
});

注意我还将标签文本设置为灰色

如果您想将所有其他框灰显,无论您是什么类别,都可以这样做

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

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

关于javascript - 检查一组复选框是否在 Bootstrap 中选中。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23884139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com