dimanche 6 août 2017

Select all checkbox to be checked if all checkboxes are checked

I need my selectAll checkbox to be checked if all the other checkboxes are checked. Any idea how to do this in javascript?

My javascript code:

function selectAll(box) {
    var selectAllchk = box.checked;
    var checkboxes = document.getElementsByName('chkbox');
    if (box.checked){
        for (var i in checkboxes){
            $(":checkbox").attr("checked", true);
        }
    } else {
        $('input:checkbox:not(:disabled)').removeAttr('checked');
    }
}


function selectCheckAll(box){
    var a = ('.checkbox').length;
    if ($(':checkbox:checked').length == $('.checkbox').length) {
       //do something
    }
};

Jsp code:

<td align="center" colspan="5">
  <display:table name="contratBonusForm" property="contrats" defaultsort="1" 
  sort="list" defaultorder="ascending" requestURI="/gestionContratBonusActionIn.do?dispatch=searchContrat" 
  uid="current" pagesize="30" class="tableauDisplayTag">
   <display:column title="ID Type" property="id" sortable="true"/>
   <display:column title="Type de contrat" property="type" sortable="true"/>
  <% %>
  <display:column title="Bonus <input type='checkbox' name='selectall' onClick='selectAll(this)'/>" align="center" >
  <% TypeContratPOJO type = (TypeContratPOJO) pageContext.getAttribute("current");
  Long Eli = 1L;
  int ok = type.getBonus().compareTo(Eli);
  (current != null &&  ok == 0) { %>
  <input type="checkbox" name="chkbox" checked="checked" onClick='selectCheckAll(this)' value="<%=indice%>"/>
  <% } else { %>
  <input type="checkbox" name="chkbox" value="<%=indice%>"/>
  <% } %>
  </display:column>
  <%indice++; %>            
  </display:table>
</td>

When I select a checkbox it does not enter the selectCheckAll function.




Aucun commentaire:

Enregistrer un commentaire