简体   繁体   中英

django checkbox select all

how can i select all checkbox when i click header checkbox? By javascript? How? And can i do that in easier method? thanks:D

run.html

<form name="form" method="post" action="/home/{{build}}/">
<br>
<input type="submit" value="Delete" style="margin-left:149px; width:80px; height:30px">
<input type="hidden" name="build_id" value="{{build_id}}" />
<table border="1"; style="margin-left:150px; border-collapse:collapse;margin-top:10px"; cellpadding="4" borderColor=black>
<tr bgcolor=#888888>
<td><input type="checkbox" align="center"></td>
<td><b>Run</b></td>
<td><b>Product</b></td>
</tr>

{% for run in run_list %}
    <tr>
    <td><input type="checkbox" name="var_delete" value="{{run.id}}"></td>
    <td><a href="/home/{{build}}/{{run.name}}">{{build}} {{run.name}}</a></td>
    <td>{{run.build.version}}</td>
    </tr>
{% endfor %}
</table>
</form>

Check this one: you'll be able to toggle master checkbox selection as well in case you click on item checkbox:

$(function() {

  var masterCheckbox = $('#select_all');
  var slaveCheckboxes = $('.checkbox_delete');

  masterCheckbox.click(function() {
    slaveCheckboxes.attr('checked', masterCheckbox.attr('checked'));
  });

  slaveCheckboxes.click(function() {
      // Check all slave checkboxes selections: in case all are checked - check the master checkbox as well
      masterCheckbox.attr('checked', $.grep(slaveCheckboxes, function(e) {
          return $(e).attr('checked');
      }).length == slaveCheckboxes.length);
  });

});

You've no way but Javascript . Once the page is loaded you cannot do anything by django. A stupid method can be to attach a handler to the master checkbox to reload the page and set them checked by django but it's a very very bad idea.

If you're using some library like mootools or jquery it's easy to achieve.

As second hint, take a look at django forms , they make your life easier when working with everything near forms.

You have to use javascript for that. This is an example using jquery...

$(".checkbox_delete").attr('checked', true);

put that in the click event of the header checkbox and add that class (checkbox_delete) to all the checkboxes you want checked.

I use jquery and write this, but it is nouse when i click 'select_all'...

run.html

<script type="text/javascript" >
$(document).ready( function () {
  $('#select_all').click( function() {
    $(".checkbox_delete").attr('checked', true);
  });
}
</script>

<form name="form" method="post" action="/home/{{build}}/">
<br>
<input type="submit" value="Delete" style="margin-left:149px; width:80px; height:30px">
<input type="hidden" name="build_id" value="{{build_id}}" />
<table border="1"; style="margin-left:150px; border-collapse:collapse;margin-top:10px"; cellpadding="4" borderColor=black>
<tr bgcolor=#888888>
<td><input id="select_all" type="checkbox" align="center"></td>
<td><b>Run</b></td>
<td><b>Product</b></td>
</tr>

{% for run in run_list %}
    <tr>
    <td><input type="checkbox" name="var_delete" value="{{run.id}}" class="checkbox_delete"></td>
    <td><a href="/home/{{build}}/{{run.name}}">{{build}} {{run.name}}</a></td>
    <td>{{run.build.version}}</td>
    </tr>
{% endfor %}
</table>
</form>

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