Opened 10 years ago
Closed 10 years ago
#14571 closed bug (duplicate)
bug about checkbox in 1.10.2 and 2.0.2
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | undecided | Milestone: | None |
Component: | unfiled | Version: | 1.10.2 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> <script src ="jQuery/jquery-1.10.2.min.js"></script> </head> <body> <input id="allChk" type="checkbox" /><label for ="allChk">AllCheck</label><br /> <input name="chk" id="Chk1" type="checkbox" /><label for ="Chk1">Check1</label><br /> <input name="chk" id="Chk2" type="checkbox" /><label for ="Chk2">Check2</label><br /> <input name="chk" id="Chk3" type="checkbox" /><label for ="Chk3">Check3</label><br /> <input name="chk" id="Chk4" type="checkbox" /><label for ="Chk4">Check4</label> </body> </html> <script type="text/javascript"> $("#allChk").click(function () { $("input[name='chk']").attr('checked', this.checked); alert($('#Chk4').attr('checked')); }); </script>
When i click the 'AllCheck' third time,check4's attribute shows "checked",but it's not checked in the page,it also occured in 2.0.2, but 1.8.3 works well.
Note: See
TracTickets for help on using
tickets.
You are not using the jQuery functions as they should be used.
To check something using the attr functions, do
$elem.attr("checked", "checked")
, not
$elem.attr("checked", true)
.
To uncheck something using the attr functions, do
$elem.removeAttr("checked")
, not
$elem.attr("checked", false)
.
If your jQuery version supports prop, then please use
$elem.prop("checked", true)
to check and
$elem.prop("checked", false)
to uncheck.
To check if something is checked, use
$elem.is(":checked")
. Never read the attribute, becasue an element with attribute checked empty IS checked, but attr returns empty string and seems unchecked.