1

Please check the below given fiddler.

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...

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.

maging a update scenario of a form where u already have one item selected on page load.

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

4

2 回答 2

4

你可以用一行代码做到这一点:

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

此外,您应该绑定change()事件,而不是click(),以便如果复选框的状态通过单击以外的方法(例如按键)更改,则将执行代码。

这是一个小提琴


编辑:如果您希望代码在页面首次加载时运行,只需在绑定后触发事件(您还必须修改函数以使其正常工作):

$('.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

这是另一个小提琴

于 2013-08-07T07:52:50.257 回答
3

尝试这个

演示

$(".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);
    }
});
于 2013-08-07T07:53:13.300 回答