我有一组复选框,我想限制在其中检查最多一个复选框。如果需要更改选择,则首先选中的选项需要取消选中,但最大限制必须为1。 这里是jQuery代码。复选框检查事件没有得到阻止
$('#ReportRow').on('click', 'input[type="checkbox"]', (function (event) {
alert("Hi");
var checkedReportValues = $('#ReportRow input:checkbox:checked').map(function() {
return this.value;
}).get();
if ($("#ReportRow input:checkbox:checked").length > 1) {
return false;
}
alert(checkedReportValues);
})
);
这里,上面的代码被限制只有一个被选中的复选框,但是当我试图检查等,他们首先被检查,然后选中。我在哪里做错了?
这里是动态创建的HTML。
//Add Code to Create CheckBox dynamically by accessing data from Ajax for the application selected above
var Reports = " User, Admin, Detail, Summary";
var arrReportscheckBoxItems = Reports.split(',');
var reportscheckBoxhtml = ''
for (var i = 0; i < arrReportscheckBoxItems.length; i++) {
reportscheckBoxhtml += ' <label style="font-weight: 600; color: #00467f !important;"><input type="checkbox" value=' + arrReportscheckBoxItems[i] + '>' + arrReportscheckBoxItems[i] + '</label>';
}
//Add Submit button here
reportscheckBoxhtml += ' <button type="button" id="SubmitReport" class="btn btn-primary">Submit</button>';
$('#ReportRow').html(reportscheckBoxhtml);
提供您的HTML? –
'如果选择需要改变,那么首先选中的选项需要取消选中,但最大限制必须是1。'使用'' –
@MayankPandey添加了HTML – Lara