开发者

how to show and hide image button on checkbox

开发者 https://www.devze.com 2023-01-05 05:14 出处:网络
$(\'#btnSelectAll\').click(function() { $(\'.inputchbox\'开发者_StackOverflow中文版).attr(\'checked\', true);
   $('#btnSelectAll').click(function() {
            $('.inputchbox'开发者_StackOverflow中文版).attr('checked', true);
            $('fieldset').find('img').hide(); // datepicker hide

        });
   //Cancel will deselect all checkboxes and hide submit and Cancel Buttons
        $('#btnCancel').click(function() {
            $('.inputchbox').attr('checked', false);
             $('fieldset').find('img').show(); //datepicker show
        });

This is my selectAll and cancelall click events..its working good..

other then this On individual selection on checkbox

 $(".inputchbox").change(function() {
        if ($('.inputchbox').is(':checked')) {
            $(this).closest("fieldset").find('img').hide(); // Datepicker hide
        }
        else {
            $(this).closest("fieldset").find('img').show(); // datepicker show

        }
    });
</script>

on selctAll and cancelall i can show and hide my all datepicker image in my fieldset

on individual selction of checkbox is also working good.

my problem is..

when I selectAll i can hide all my datepicker image after doing is.. if do Individually on my each checkbox box to show my Image its not working and not even going in to else condition on checkbox selection? is that anything doing wrong thanks


Right off the bat, I think you want to change the if statement to use $(this) instead of the class selector .inputchbox, that should at least get your else to trigger

 $(".inputchbox").change(function() {
        if ($(this).is(':checked')) {
            $(this).closest("fieldset").find('img').hide(); // Datepicker hide
        }
        else {
            $(this).closest("fieldset").find('img').show(); // datepicker show

        }
    });


$(".inputchbox").change(function() {
        if ($('.inputchbox').is(':checked')) {
  //    use this here ^---< $(this).is(':checked')


As an alternative to the accepted answer, there's rarely a reason to this .is(':checked') in an if statement, you can use the DOM .checked property directly, and .toggle(bool) instead of show/hide, like this:

$(".inputchbox").change(function() {
  $(this).closest("fieldset").find('img').toggle(!this.checked);
});
0

精彩评论

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