我在表单中使用复选框列表。我在复选框列表中有多个选项。选中其中一个选项时,应禁用其他选项,以便不允许用户选择其他选项。
我已经在c#中完成了,但是我不能再使用回发了,我们怎么可以在javascript或jquery中这样做呢?
foreach (ListItem li in chk_mycheckbox.Items)
{
if ((!(li.Text.ToString().Contains("Unknown"))))
{
li.Enabled = false;
}
}发布于 2014-02-13 19:01:36
你就是这样做的
$(document).ready(function () {
var checked = false;
$('#<%=lstExposureValue.ClientID%> input:checkbox').click(function () {
var currentIdone = 'Unknown';
var currentId = $(this).next().html();
if (currentId == currentIdone) {
if (checked) {
$("#<%=lstExposureValue.ClientID%> input").removeAttr('disabled');
checked = false;
return;
}
else {
$("#<%=lstExposureValue.ClientID%> input").attr('checked', false);
$(this).attr('checked', true);
$('#<%=lstExposureValue.ClientID%> input:not(:checked)').attr('disabled', 'disabled');
checked = true;
}
}
});
});发布于 2014-02-11 16:37:35
你可能真的想要一组单选按钮。
例如:
<li><input type="radio" name="gender" value="male">Male</li>
<li><input type="radio" name="gender" value="female">Female</li>因为它只会强制执行一个选择。
但是,在当前的方案中,您可以使用以下行循环检查复选框,并禁用未选中的任何复选框。(这是使用jquery 1.6+)
$('input[type=checkbox]').each(function () {
if(!$(this).is(':checked')){
//if not checked
$(this).prop('disabled', true);
}
});以下是你的评论。
给那些应该禁用其他人的勾选框一个额外的类,例如“单一选择”。然后,您的代码可以如下所示:
$(document).ready(function() {
$('#formId input.single-selection').on('click', function (e) {
var selectedCheckbox = $(this);
if(selectedCheckbox.is(':checked')){
$('#formId input[type=checkbox]').each(function () {
if(selectedCheckbox[0] != $(this)[0]){
//if not selected checkbox
$(this).prop('disabled', true);
$(this).attr('checked', false);
}
});
}else{
$('#formId input[type=checkbox]').prop('disabled', false);
}
});
});我没有测试过,但我相信这是你需要的。只需将ids和类替换为所使用的内容即可。
发布于 2014-02-11 16:51:55
假设您希望在用户取消选择所选选项的情况下启用其他选项,则可以尝试此代码。代码假定窗体的ID为“form1”
$('#form1 input').on('click',function(evt){
var clickedEl = evt.target;
if(clickedEl.checked){
$('#form1 input').each(function(){
$(this).attr('disabled',true);
});
$(clickedEl).attr('disabled',false);
}
else{
$('#form1 input').each(function(){
$(this).attr('disabled',false);
});
}
});注意,在jquery中,每个函数允许您传递一个函数,该函数将在选择器匹配的每个元素上调用。
下面是对代码工作的修改:http://jsfiddle.net/vvt5g/
https://stackoverflow.com/questions/21707215
复制相似问题