Home  >  Article  >  Web Front-end  >  jquery cannot set the checkbox to be selected or not become selected_jquery

jquery cannot set the checkbox to be selected or not become selected_jquery

WBOY
WBOYOriginal
2016-05-16 16:54:141350browse
Copy code The code is as follows:

$("input").attr("checked","checked ")

After setting, the checkbox did not become selected. I checked it with chrome debugging. There is indeed a checked attribute in the checkbox, and the value is checked, but the page display is still unselected
Copy code The code is as follows:

$("input").prop("checked", true);


The difference between attributes and properties is important in certain situations. Before jQuery 1.6, the .attr() method would return the value of property when taking the value of certain attributes, which led to inconsistent results. Starting from jQuery 1.6, the .prop() method returns the value of the property, and the .attr() method returns the value of the attributes.

For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should be retrieved or assigned using the .prop() method. Before jQuery 1.6, these attributes were obtained using the .attr() method, but this was not the attr attribute of the element. They have no corresponding attributes, only properties.

For example, consider a DOM element defined in the HTML tag , and assume it is a JavaScript variable named elem :

elem.checked true (Boolean) will change the status of the check box
$(elem).prop("checked") true (Boolean) will change the status of the check box
elem.getAttribute("checked ") "checked" (String) The initial state of the check box that will not change;
$(elem).attr("checked") (1.6) "checked" (String) The initial state of the check box that will not change Initial state;
$(elem).attr("checked") (1.6.1 ) "checked" (String) will change the state of the checkbox
$(elem).attr("checked") ( pre-1.6) true (Boolean) will change the state of the checkbox
According to the W3C form specification, the checked attribute is a Boolean attribute, which means that as long as the attribute exists, even if it has no value or is a null character String, the property corresponding to this attribute is true. The following recommended method is compatible with browsers and determines whether the checked attribute of the checkbox element is "true":
Copy code Code As follows:

if ( elem.checked )
if ( $(elem).prop("checked") )
if ( $(elem).is(":checked" ) )

If you use jQuery 1.6, the code if ( $(elem).attr("checked") ), will get an attribute (attribute), which does not change whether the checkbox is checked and selected. It is only used to store the initial value of the default or selected property. In order to maintain backward compatibility, the .attr() method starting from jQuery 1.6.1 will also update the property attribute in addition to returning the attribute value, so the boolean attribute does not need to change its value through .prop(). It is recommended to use one of the above methods to obtain the value of checked.
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