I have pagination that comes with checkbox in first column and each row.

If any of checkbox is checked it will automatic slidedown full wide box from top, I use jquery here is code...

$(':checkbox').change(function() {
    // use the :checked selector to find any that are checked
    if ($('#checkbox').length) {

        $(".checkbox-tools").animate({top: '0px'});

    } else {
        $('.checkbox-tools').slideUp('slow');
    }
 });
  <!--Cancel print button.-->   

$(document).ready(function(){
  $("#cancel-chedkbox").click(function(){
    $(".checkbox-tools").animate({top: '-450px'});
     $('input:checkbox').removeAttr('checked');
  });});

This works great when checked on checkbox and slide down.

What if person UNCHECKED and there is no checked in checkbox and I want to automatic slideup full width box instead of person click close button if there is no checked at all.

How can I solve that!

AM

有帮助吗?

解决方案

For uncheck checkboxes use:

$('input:checkbox').prop('checked', false)

To check checkboxes use:

$('input:checkbox').is(':checked')

To count checked checkboxes use:

$('input:checked').length

It's similar post on: checking if a checkbox is checked?

UPDATE:

In Your code, all checkboxes have ID #checkbox - ID have to be unique, here You have working code http://jsfiddle.net/s8Xju/1/:

$(':checkbox').change(function() {
    // use the :checked selector to find any that are checked
    if ($(':checkbox:checked').length > 0) {

        $(".checkbox-tools").animate({top: '200px'});

    } else {

        $('.checkbox-tools').animate({top: '-450px'});
    }
 });

其他提示

To check a checkbox:

$('input:checkbox').prop('checked', true)

To uncheck a checkbox:

$('input:checkbox').prop('checked', false)

To check whether particular checkbox is checked or not:

$('input:checkbox').is(':checked')

This statement returns true if checked and returns false if unchecked.

This is a bit late but I thought I would put some jquery code that I like to use when checking to see if a box is checked or unchecked. The above answer should work fine, but if you don't want to use .length for some reason you can use this.

to see if a checkbox is checked I use:

$("#checkboxID").is(':checked') - returns true - false    

so to see if a checkbox is not checked I just do this:

!($("#checkboxID").is('checked') - returns true - false but inverses it

Im not sure if this is bad practice or anything along those lines but it works perfect for me.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top