简体   繁体   中英

django checkbox select all by jquery

I want to select all checkbox when i click the top check, below is my code:

run.html

<script language="JavaScript" src="media/js/jquery-1.4.2.min.js"></script>
<script language="javascript">
$("#selectAll").change(function() {
  $(".xxx:checkbox").attr('checked', this.checked);
});
</script>
Select All: <input type="checkbox" id="selectAll"><br />
<br />

One: <input type="checkbox" class="xxx" /><br />
Two: <input type="checkbox" class="xxx" /><br />
Three: <input type="checkbox" class="xxx" />

but why it not work?

Because selectAll does not exist at the time the <script> is run. So $("#selectAll") matches no elements. (jQuery doesn't warn you when you apply an operation to no elements, it just silently does nothing.)

Put the <script> below the <input> , or put the binding in a $(document).ready(function() { ... }); block to make the code run at page load time.

Aside: I would avoid use of the non-standard jQuery selectors like :checkbox wherever possible, as they force the use of the JavaScript Sizzle selector library instead of fast native querySelectorAll . input.xxx[type=checkbox] would be another way of saying it.

Below is the latest version, it can work

<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.js'></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.js"></script> 

<script type='text/javascript'> 
$(document).ready(function(){
    $("#selectAll").change(function() {
        $(".checkbox_delete:checkbox").attr('checked', this.checked);
    });
});
</script> 

Alternative to $(document).ready(function(){ .... you can use the jQuery-Function "LIVE":

$("#selectAll").live('change',function() {
  $(".xxx:checkbox").attr('checked', this.checked);
});

You can test this on: http://jsfiddle.net/GLTQQ/

btw, the following:

$(function(){
    $("#selectAll").change(function() {
        $(".checkbox_delete:checkbox").attr('checked', this.checked);
    });
});

is the shortcut for:

$(document).ready(function(){
    $("#selectAll").change(function() {
        $(".checkbox_delete:checkbox").attr('checked', this.checked);
    });
});

Here is a simple example for select all Checkbox.....

<ul>
<li><input type="checkbox" id="select_all"/> Selecct All</li>
<li><input class="checkbox" type="checkbox" name="check[]"> This is Item 1</li>
<li><input class="checkbox" type="checkbox" name="check[]"> This is Item 2</li>
<li><input class="checkbox" type="checkbox" name="check[]"> This is Item 3</li>
<li><input class="checkbox" type="checkbox" name="check[]"> This is Item 4</li>
<li><input class="checkbox" type="checkbox" name="check[]"> This is Item 5</li>
<li><input class="checkbox" type="checkbox" name="check[]"> This is Item 6</li>

<script>
  var select_all = document.getElementById("select_all"); //select all checkbox
   var checkboxes = document.getElementsByClassName("checkbox"); 
   //checkbox items

  //select all checkboxes
  select_all.addEventListener("change", function(e){
  for (i = 0; i < checkboxes.length; i++) { 
    checkboxes[i].checked = select_all.checked;
 }
});


for (var i = 0; i < checkboxes.length; i++) {
checkboxes[i].addEventListener('change', function(e){ //".checkbox" change 
    //uncheck "select all", if one of the listed checkbox item is unchecked
    if(this.checked == false){
        select_all.checked = false;
    }
    //check "select all" if all checkbox items are checked
    if(document.querySelectorAll('.checkbox:checked').length == checkboxes.length){
        select_all.checked = true;
    }
});
 }
 </script>

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