简体   繁体   中英

Show Hide button, atleast one checkbox is checked

When I check one or many boxes it shows the buttons "btns" but if i uncheck one it hides them, how do I get it to stay if one or many checkbox is still checked

<input type='checkbox' class='checklist' name='id[]' value='$id' />
<input type='checkbox' class='checklist' name='id[]' value='$id' />
<input type='checkbox' class='checklist' name='id[]' value='$id' />
<input type='checkbox' class='checklist' name='id[]' value='$id' />

$(".btns").hide();

$(".checklist").click(function() {
    if($(this).is(":checked")) {
        $(".btns").show();
    } else {
        $(".btns").hide();
    }
});

You can check for the amount of checked checkboxes. If it's length is 0 - hide the element, if not - show it.

 $(".checklist").click(function() { $('.btns').toggle( $(".checklist:checked").length > 0 ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='checkbox' class='checklist' name='id[]' value='$id' /> <input type='checkbox' class='checklist' name='id[]' value='$id' /> <input type='checkbox' class='checklist' name='id[]' value='$id' /> <input type='checkbox' class='checklist' name='id[]' value='$id' /> <p class='btns' hidden>btns</p> 

$(".checklist").click(function() {
var check_count=$(".checklist:checked");
if(check_count.length>0){$(".btns").show();}
else{$(".btns").hide();}
});

It check if any of checklist check so show .btns else hide it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM