gpt4 book ai didi

javascript - 如何使用 angular 或 jquery 限制复选框的选择

转载 作者:太空宇宙 更新时间:2023-11-04 15:03:00 25 4
gpt4 key购买 nike

我正在尝试按照那里的所有示例进行操作,但我并不走运。我使用 jquery 验证检查创建了一个 plunker。我只需要允许从组中选择 1 个复选框。 plunker

  <body ng-controller="MainCtrl">
<div class="form-group fg-line">
<label for="companies" class="control-label"><b>Role Type</b></label><br /> <br />
<table>
<tr ng-repeat="model in userRoles">
<td>
<div class="checkbox m-b-15">
<label>
<input id="rolebox" class="check" type="checkbox" ng-model="model.RoleId">
<i class="input-helper"></i>
{{model.Name}}
</label>
</div>
</td>
</tr>
</table>
</div>
<script>
var checked = [],
$check = $('.check').change(function () {
if (this.value == -1 && this.checked) {
$check.not(this).prop('disabled', true).prop('checked', false);
checked = [];
}
else {
$check.prop('disabled', false);
checked.push(this);
checked = $(checked)
checked.prop('checked', false).slice(-2).prop('checked', true);
}
});

最佳答案

I need to only allow 1 checkbox selection out of the group.

正如在对您的问题的评论之一中提到的,您真的应该为此目的使用单选按钮,因为它们就是为这种情况而设计的。如果您必须使用复选框,则可以执行以下操作。

$(document).on("change", ".check", function() {
var $allCheckboxes = $(".check");
$allCheckboxes.prop("disabled", false);
this.checked && $allCheckboxes.not(this).prop("disabled", true);
});

Your updated plunker

关于javascript - 如何使用 angular 或 jquery 限制复选框的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31676162/

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