attr of jquery Get form checked Boolean problem

Source: Internet
Author: User



<!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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.