Implementation code of jQuery all select and all not select event binding

  • 2021-07-13 04:22:39
  • OfStack

No more nonsense, just post the code for everyone. The specific code is as follows:


<td width="82%" colspan="3">
<input type="checkbox" id="all"> All selection &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" id="reverse"> Reverse selection 
</td>
<td width="82%" colspan="3">
<s:checkboxlist name="resUuids" list="resList" listKey="uuid" listValue="name"></s:checkboxlist>
</td>
$(function(){
// All selection 
$("#all").click(function(){
// Select all the following components 
//$("[name=resUuids]")   Is multiple components, and the whole is an array of objects 
//$("[name=resUuids]").attr("checked","checked");
// Get the state of the current component first 
//$(this).attr("checked")
// Set all components to the corresponding state 
//$("[name=resUuids]").attr("checked",$(this).attr("checked"));
//$(this).attr("checked") What exactly is the value obtained 
//alert($(this).attr("checked"));    //undefined
//$("[name=resUuids]").attr("checked","undefined");
//js Grammar rules, except false,FALSE,"false","FALSE",05 All values except values are identified as true
//$("[name=resUuids]").attr("checked",false);
var flag = $(this).attr("checked");
$("[name=resUuids]").attr("checked",flag == "checked");
});
// Reverse selection 
    $("#reverse").click(function(){
      // Switch the state of all components to the reverse state of the original state 
      //$("[name=resUuids]").attr("checked",!($("[name=resUuids]").attr("checked")=="checked"));
      // When multiple components are selected by the selector, any data obtained from the component is compared with the 1 Components to operate 
      //alert(!($("[name=resUuids]").attr("checked")=="checked"));
      // Iterate each component so that its operation state is inverse of the original state of the corresponding component 
      $("[name=resUuids]").each(function(){
        // Use each Operation implements the operation on each component 
        var flag = $(this).attr("checked"); 
        $(this).attr("checked", !(flag =="checked"));
      });
      checkSelect();
    });
// Binding component 
    $("[name=resUuids]").click(function(){
      // Set the selected all state to a comprehensive state value based on all components 
      checkSelect();
    });
    function checkSelect(){
      var allFlag = true;
      $("[name=resUuids]").each(function(){
        var flag = $(this).attr("checked") == "checked";
        //&: Bit operation and    &&: Logical and 
        allFlag = allFlag && flag; 
      });
      $("#all").attr("checked",allFlag);
    }
  });

Related articles: