2013-10-15 27 views
0

在以下代碼中 - 如何執行檢查並取消選中。現在它只做一件事:On Off Switch - 複選框

$('#div_group').click(function() { 
     var status = $(this).attr('checked'); 
     if(! status) { 
     status = false; 
     } 
     $("input[id~='div_group']").attr('checked', true); 

    }); 

回答

1

使用.prop而不是.attr

$('#div_group').click(function() { 
    var $this = $(this); 

    $("input[id~='div_group']").prop('checked', $this.is(':checked')); 
}); 

爲什麼? http://api.jquery.com/prop/

+0

謝謝。道具做了魔術。 – New2JQ

3

你需要使用道具而不是attr。 嘗試...

$("input[id~='div_group']").prop('checked', true); 

,然後取消選中它設置truefalse

0

您可以使用.is()在這種情況下看到的元素的:checked財產。這樣做之後,你應該使用.prop()的元素:checked財產分別設置爲真正

$('#div_group').on('click', function() { 
    if($(this).is(':checked')) { 
     $("input[id~='div_group']").prop('checked', false); 
    } else { 
     $("input[id~='div_group']").prop('checked', true); 
    } 
});