开发者

uncheck all checkboxes except the one with a class?

开发者 https://www.devze.com 2023-01-01 04:24 出处:网络
hi i try to check/uncheck all checkboxes that don\'t got a class to them. This is the code I got but it check the class boxes to.

hi i try to check/uncheck all checkboxes that don't got a class to them. This is the code I got but it check the class boxes to.

$(document).ready(function() {
        $('#ctl00_ContentPlaceHolder1_repReceivers_ctl00_chxAll').click(
         funct开发者_运维知识库ion() {
        if (!$("input[type='checkbox']").hasClass("testclass")) {
             $("input[type='checkbox']").attr('checked', $('#ctl00_ContentPlaceHolder1_repReceivers_ctl00_chxAll').is(':checked'));
         }});
    });

any ideas where I go wrong?

EDIT

This suck but asp.net add a span around the checkbox input when given a cssclass, so the checkbox itself dont get the class. I tried to do like you say but like .not(thecheckboxid) but no luck.

EDIT CODE

<span class="testclass"><input id="ctl00_ContentPlaceHolder1_chxMale" type="checkbox" name="ctl00$ContentPlaceHolder1$chxMale" /></span>


You need to modify your code a bit, like this (updated for updated question):

$(function() {
  $('#ctl00_ContentPlaceHolder1_repReceivers_ctl00_chxAll').click(function() {
    $(":not(.testclass) > input[type='checkbox']").attr('checked', this.checked);
  });
});

Currently you're checking if the first matched checkbox has the class, you want to filter out all those without the class from the batch you're setting the checked property on. There are a few methods to do this, but :not() or .not() are the easiest here. The other change above is that since you're inside an event handler for the _chxAll checkbox, you can use this instead of repeating the ID, it's shorter and faster :) With this you can use the .checked DOM property as well.


I added a few much-needed performance improvements.

var inputs = $('input:checkbox').not('.testclass');

$('#ctl00_ContentPlaceHolder1_repReceivers_ctl00_chxAll').click(
    function() {
        inputs.attr('checked', this.checked);
    }
);


$('input:checkbox').not('.testclass').attr('checked', $('#ctl00_ContentPlaceHolder1_repReceivers_ctl00_chxAll').is(':checked'));
0

精彩评论

暂无评论...
验证码 换一张
取 消