jquery Control checkbox selected but does not show selected

Source: Internet
Author: User

Problem Description: Use jquery to control the checkbox selection, but the first click appears with the selected style, then click to see checked's properties increased successfully but did not select
Medium state.

Problem code:

function Chooseallornot (id) {var choose=$ ("Input[name= '" +id+ "']"). attr (' checked '); if (choose== ' checked ') {$ ("input[ Type=checkbox][name= ' "+id+" '] "). Removeattr (' checked '); choose=$ (" Input[name= ' "+id+" '] "). attr (' checked ');} else{$ ("Input[type=checkbox][name=" "+id+" "]"). attr (' checked ', ' true '), choose=$ ("Input[name= '" +id+ "']"). attr (' Checked ');}}

  


Workaround:

function Chooseallornot (id) {var choose=$ ("Input[name= '" +id+ "']"). Prop (' checked '); if (choose) {$ ("input[type= Checkbox][name= ' "+id+" '] "). Removeprop (' checked '); choose=$ (" Input[name= ' "+id+" '] "). Prop (' checked ');} else{$ ("Input[type=checkbox][name=" "+id+" "]"). Prop (' checked ', true); choose=$ ("Input[name= '" +id+ "']"). Prop (' Checked ');}}

  


Or use JavaScript native code to implement

var fir = document.getelementsbyname ("name"); for (Var i=fir;i<fir.length;i++) {fir[i].checked = true;}

  


Summary: Use attr () to assign a value to the attribute checked, and find that whatever value is assigned to the property, as long as it is selected, use the. attr (' checked ', ' true ');
and. attr (' checked ', ' false '), the results are all selected, either attr () and removeattr () can be used, or use. attr (' checked ', true);
(' checked ', false), but with attr (), only the first occurrence of the selected and unchecked state, followed by the unchecked state but the code display controls the Checked property
Only use prop () or JavaScript native code to control it, this is not the case.

Report:

When to use attr (), when to use Prop ()?
1. Add property name This property will take effect should use Prop ();
2. There are true,false two attributes using prop ();
3. The other uses attr ();

jquery Control checkbox selected but does not show selected

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.