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