i am using a checkbox list in a form. i have multiple options in the checkbox list. When one of the option is checked then other options should be disabled so that users should not be allowed to select other options.

我在表单中使用复选框列表。我在复选框列表中有多个选项。选中其中一个选项后,应禁用其他选项,以便不允许用户选择其他选项。

i have done it in c# in the eventhandler but i cannot use postback anymore how can we do this in javascript or jquery

我已经在事件处理程序中用c#完成了但是我不能再使用postback如何在javascript或jquery中执行此操作

    foreach (ListItem li in chk_mycheckbox.Items)
            {

                if ((!(li.Text.ToString().Contains("Unknown"))))
                {
                    li.Enabled = false;

                }

            }

6 个解决方案

#1


4

Might you actually want a set of radio buttons instead.

可能你实际上想要一组单选按钮。

e.g.

<li><input type="radio" name="gender" value="male">Male</li>
<li><input type="radio" name="gender" value="female">Female</li>

As it will enforce only one selection.

因为它只会强制执行一个选择。

However, in your current scenario you could use the following lines to loop over the checkboxes and disable any that are not selected. (this is using jquery 1.6+)

但是,在当前场景中,您可以使用以下行循环复选框并禁用任何未选中的行。 (这是使用jquery 1.6+)

$('input[type=checkbox]').each(function () {
    if(!$(this).is(':checked')){
        //if not checked
        $(this).prop('disabled', true);
    }
});

Following on from your comment.

继续你的评论。

Give the tick boxes that should disable the others an extra class e.g. "single-selection". Then your code can look something like this:

给出应该禁用其他类别的复选框,例如“单项选择”。然后你的代码看起来像这样:

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

I've not tested it, but I believe this is what you need. Just replace the ids and classes with what you're using.

我没有测试过,但我相信这就是你需要的。只需用您正在使用的内容替换ID和类。

更多相关文章

  1. 笨鸟求问,下拉列表实现局部刷新的问题
  2. 如何将加载微调器图像添加到jquery选项卡中的每个选项卡?
  3. jqueryui autocomplete 插件 点击 显示选项设置方法
  4. 在没有重新设置源选项的情况下,使用x-editable更改select2的数据
  5. 将JavaScript数组转换成逗号分隔列表的简单方法?
  6. 在Chrome中使用AJAX发送选项而不是GET/POST/PUT/DELETE?
  7. 使用Ajax+JQuery构造分页查询列表
  8. 如何使用jQuery选择列表中的最后X项?
  9. jquery不会对select/选项更改事件作出反应。

随机推荐

  1. linux内核段属性机制(以subsys_initcall
  2. 在Ubuntu Linux 安装Python第三方库:NumP
  3. wifidog 源码初分析(2)
  4. Linux系统下Tar文件安装方法
  5. 在linux下安装并使用websocket
  6. 【linux学习笔记】vim命令小结
  7. [嵌入式Linux驱动]S5PV210的蜂鸣器Linux
  8. [置顶] 每天进步一点点——Linux
  9. c中变参函数的理解和编写(hello world引发
  10. Linux编程之《只运行一个实例》