0

If i have checked list 0 and i am going to check any one from the sibling either list 1 nor list 2 . I have tried with at least check one from the list.

    <div><input name="list[]" id="list0" type="checkbox"  value="newsletter0" >zero</div>
    <div><input name="list[]" id="list1" type="checkbox"  value="newsletter1" >one</div>
    <div><input name="list[]" id="list2" type="checkbox"  value="newsletter2" >two</div>          
4

2 回答 2

0

另一种可能的解决方案http://jsfiddle.net/7zQtE/

HTML

    <div><input name="list[]" id="list0" type="checkbox" value="newsletter0" />zero</div>
    <div><input name="list[]" id="list1" type="checkbox" value="newsletter1" disabled="disabled" />one</div>
    <div><input name="list[]" id="list2" type="checkbox" value="newsletter2" disabled="disabled" />two</div>
    <!-- Added button -->
    <input type="button" id="btn" value="Check" />

Javascript

$(document).ready(function(){
    $('#list0').on('change', function(){
        $(this).prop('checked') ? $('#list1,#list2').removeAttr('disabled') : $('#list1,#list2').attr('disabled', 'disabled').removeAttr('checked');
    });

    $('#list1,#list2').on('change', function(){
        $(this).prop('checked') ? $('input[name=list\\[\\]]:not(:checked)').attr('disabled', 'disabled') : $('input[name=list\\[\\]]:not(:checked)').removeAttr('disabled');
    });

    //Added button handler
    $('#btn').on('click', function(){
        if($('#list0').prop('checked') && !$('#list1,#list2').prop('checked')){
            alert('check item 1 or 2');
        }    
    })
});
于 2013-09-09T05:37:54.393 回答
0

希望我正确理解您的问题:如果用户检查零,您希望他们能够检查一或二,但不能同时检查一和二?

请参阅此示例。希望它能够满足您的需求,或者至少向您展示如何实现您想要的:

http://jsfiddle.net/3ABJC/

$('#list0,#list1,#list2').change(function() {
    if ($('#list0').is(':checked')) {
        // if either one of list1 or list2 is checked
        if ($('#list1:checked,#list2:checked').length == 1) {
            // disable the one that is not checked
            $('#list1:not(:checked),#list2:not(:checked)').prop('disabled',true);
        } else {
            // neither are checked, make sure they are all enabled
            $('#list1,#list2').prop('disabled', false);
        }
    } else {
        // if both are one and two are checked
        if ($('#list1:checked,#list2:checked').length == 2) {
            // disable zero (so you can't have all 3 checked)
            $('#list0').prop('disabled', true);
        } else {
            // otherwise, make sure all are enabled
            $('#list0,#list1,#list2').prop('disabled', false);
        }
    }
});
于 2013-09-09T05:21:41.957 回答