Using JQuery to achieve all and cancel the two simple methods

  • 2020-03-30 02:07:10
  • OfStack

Using JQUERY to achieve all selection and cancel all selection, not as cumbersome as js, and support more browsers.

<mce:script type="text/javascript"><!--

$(function() {  
    $("#checkall").click(function() {  
        $("input[@name='checkname[]']").each(function() {  
            $(this).attr("checked", true); 
        }); 
    }); 
    $("#delcheckall").click(function() {  
        $("input[@name='checkname[]']").each(function() {  
            $(this).attr("checked", false); 
        }); 
    }); 
}); 
// --></mce:script> 

<input type='checkbox' id='id1' name='checkname[]' value='1' />value1  
<input type='checkbox' id='id2' name='checkname[]' value='2' />value2 
<input type='checkbox' id='id3' name='checkname[]' value='3' />value3 

<input type="button" id="checkall" name="checkall" value=" Future generations " /> 
<input type="button" id="delcheckall" name="delcheckall" value=" To cancel all " />

A simpler one:

$("#checkall").click( 
  function(){ 
    if(this.checked){ 
        $("input[name='checkname']").attr('checked', true)
    }else{ 
        $("input[name='checkname']").attr('checked', false)
    } 
  } 
);

Related articles: