<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <Scriptsrc= "Http://code.jquery.com/jquery-1.11.3.js"></Script> <title>Document</title> <Scripttype= "Text/javascript"> $(function(){ $('#all'). On ('Click',function(){$('input:lt (4)'). Prop ('checked',true);//LT,GT is not including the critical, }); $('Input:eq (5)'). On ('Click',function(){$('input:lt (4)'). Prop ('checked',false); }); $('#btn'). Click (function(){var$att=$('Input:checkbox'). Prop ('checked');//var $att =$ (' Input:checkbox '). attr (' Checked ' always pops up undefined//alert ($att);if($att==false){ //$ (' input[checked=true] '). attr (' checked ', false); $('Input:checkbox'). Prop ('checked',true);}Else{$('Input:checkbox'). Prop ('checked',false);} //$ (' [Name=hobby]:checkbox '). each (function () {//this.checked=!this.checked;////assigns the opposite value. // }) }); }); </Script></Head><Body> <formAction=""> <label for= ' hobby '>The sport you love is</label><BR> <inputtype= "checkbox"name= "Hobby" >Football<inputtype= "checkbox"name= "Hobby" >Basketball<inputtype= "checkbox"name= "Hobby" >Badminton<inputtype= "checkbox"name= "Hobby" >Table tennis<BR> <inputtype= "button"value= "Select All"ID= ' All '> <inputtype= "button"value= "All is not selected"> <inputtype= "button"value= "Reverse Selection"ID= ' btn '><!--<input type= "button" value= "Submit" > - </form></Body></HTML><!--The problem is that attr get checked property values, always pop-up undefined, and later found to use prop to get the value before the full selection and anti-selection attr to set the checked value can only be set once, two with prop can be set multiple times -
1. Get the Checked property by using the Prop method, get the checked return value Boolean, check True, otherwise the flase copy the Code code as follows:<inputtype= "checkbox"ID= "SelectAll"onclick= "Checkall ()">Select all function Checkall () {var checkedofall=$ ("#selectAll"). Prop ("checked"); alert (Checkedofall); $ ("Input[name=" Procheck '] "). Prop (" Checked ", Checkedofall);} 2. If the attr method is used to obtain, if the initialization of the current input does not have the checked attribute defined, the $ ("#selectAll") will be returned regardless of whether it is currently selected. attr ("checked") returns undefined; Copy code code as follows: <inputtype= "checkbox"ID= "SelectAll"onclick= "Checkall ()" >Select All if the checked attribute is already defined in the current input, the $ ("#selectAll") is returned regardless of whether it is selected or not. attr ("checked") will return checked. Copy the code code as follows:<inputtype= "checkbox"ID= "SelectAll"onclick= "Checkall ()"checked>Select all function Checkall () {var checkedofall=$ ("#selectAll"). attr ("checked"); alert (Checkedofall); $ ("Input[name=" Procheck '] "). attr (" Checked ", Checkedofall);} To summarize, if you use jquery, you should use the Prop method to get and set the Checked property, and you should not use attr.
attr of jquery Get form checked Boolean problem