简体   繁体   English

检查/取消选中复选框列表jQuery

[英]checking/unchecking checkbox list jquery

Please check the below given fiddler. 请检查以下给定的提琴手。

http://jsfiddle.net/ymtwK/3/ http://jsfiddle.net/ymtwK/3/

i want to disable all others when one is checked... that is working as expected on the created fiddler. 我想禁用所有其他选项,当选中时...在所创建的提琴手上按预期工作。 but when i uncheck the already selected one i want want all of them to be enabled... 但是当我取消选中已经选择的一个时,我希望所有这些都被启用...

something is wrong with what my code. 我的代码有问题。 please help me to fix it. 请帮助我修复它。

i am also wondering if there anything like on unclick event is available... 我也想知道是否有类似unclick事件的东西可用...

below is my code.. 下面是我的代码。

$(".session_outcome_chk_list").click(function () {
    $('.session_outcome_chk_list').each(function () {
        if ($(this).is(":checked")) {
            $(this).removeAttr("disabled");
        } else {
            $(this).attr("disabled", true);
        }
    });
});

EDIT: 编辑:

I also require to do the same on page loads, for example on submitting with selection of one checkbox, on formload also it should check weather if one is selected or not and then do the disable. 我还要求在页面加载上执行相同的操作,例如,在选中一个复选框的情况下提交,在formload上也应检查天气(是否选中一个),然后进行禁用。

maging a update scenario of a form where u already have one item selected on page load. 想象一下表单的更新方案,其中您在页面加载时已经选择了一项。

http://jsfiddle.net/ymtwK/17/ http://jsfiddle.net/ymtwK/17/

You can do this with a single line of code: 您可以使用一行代码来做到这一点:

$('.session_outcome_chk_list').not(this).prop('disabled', this.checked);

Also, you should bind the change() event, rather than click() , so that the code will be executed if the state of the checkbox is changed via a method other than clicking (a keypress, for example). 另外,应该绑定change()事件,而不是click() change()事件,以便如果通过非单击方法(例如,按键)更改了复选框的状态,则将执行代码。

Here's a fiddle 这是一个小提琴


Edit: If you want the code to run when the page is first loaded, just trigger the event after binding it (you'd also have to modify the function in order for it to work properly): 编辑:如果您希望代码在首次加载页面时运行,请在绑定事件后触发事件(您还必须修改函数以使其正常工作):

$('.session_outcome_chk_list').change(function () {
    $('.session_outcome_chk_list').not(':checked').prop('disabled', !!$('.session_outcome_chk_list:checked').length);
}).change(); // <-- triggers the change event

Here's another fiddle 这是另一个小提琴

try this 尝试这个

DEMO 演示

$(".session_outcome_chk_list").click(function () {
    var current=$(this);
    if (current.is(":checked")) {
         $('.session_outcome_chk_list').attr("disabled", true);
         current.removeAttr("disabled");
    }
    else {
         $('.session_outcome_chk_list').attr("disabled", false);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM