Home  >  Article  >  Web Front-end  >  Solve the problem that jquery operation checkbox cannot be checked for the second time under Firefox_jquery

Solve the problem that jquery operation checkbox cannot be checked for the second time under Firefox_jquery

WBOY
WBOYOriginal
2016-05-16 17:00:591059browse

I recently used jquery to operate the checkbox at work, and used the following methods to select all and invert the selection:

Copy the code The code is as follows:

var ischecked=allCheckObj.is(':checked');
ischecked?checksObj.attr('checked',true):checksObj.attr('checked',false);

There was no problem in IE during debugging. Considering compatibility, I tried Firefox and encountered a problem. So, I did the following experiment:
The check box is bound to the click event. Click once to select, click again to uncheck, and so on. This function works fine in IE, but when tested in Firefox, there was no problem the first two times, and the selection and cancellation can be displayed normally. However, when the checkbox is selected again, the checkbox attribute value of the checkbox changes to "checked". , no problem, but the check box no longer displays the selected state. The attribute value has obviously been changed, but the check box does not display. It is too weird. I modified the code but couldn't get the correct display status. I struggled for a long time and couldn't find the reason.
Correct answer: Later, after being pointed out by the idol, it turned out to be a problem with the jQuery version. I use
$("**").attr("attrName"); while the jQuery version is 1.9, which means there is a compatibility and stability issue.
jQuery API clearly states that jQuery 1.6 needs to use prop, especially to judge the checked attribute of checkBox, that is,
Copy code The code is as follows:

$("input[type='checkbox']").prop("checked");
$("input[type='checkbox']" ).prop("disabled", false);
$("input[type='checkbox']").prop("checked", true);

will be used when using Change attr to prop and the problem is solved.
Have you encountered this strange problem? Try it quickly
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn