jQuery $(this).attr(‘checked’)出现undefined的问题
2014-03-25 13:30
369 查看
最近在对一个项目的前端进行调整时,发现项目原来用的低版本的jQuery存在一些问题,于是就把jQuery换成了比较新的v1.72,但是发现原来$(this).attr(‘checked’)返回的不再是true
or false,选中的情况下返回checked,未选中的情况返回undefined,看了jQuery文档后原来v1.6以后$(this).attr(‘checked’)就返回checked和undefined,v1.6以前返回true和false,v1.6以后可以使用$(this).is(‘:checked’)或者$(this).prop(‘checked’)来返回true和false
As of jQuery 1.6, the
attributes that have not been set. In addition,
The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the
sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the
provides a way to explicitly retrieve property values, while
For example,
and
with the
properties.
Concerning boolean attributes, consider a DOM element defined by the HTML markup
According to the W3C forms specification, the
the attribute has no value or an empty string value. The preferred cross-browser-compatible way to determine if a checkbox is checked is to check for a “truthy” value on the element’s property using one of the following:
If using jQuery 1.6, the code
the default or initial value of the checked property. To maintain backwards compatability, the
attributes is required to be changed to
转载自米豆网
本文链接地址: jQuery $(this).attr(‘checked’)出现undefined的问题
本文固定链接:http://www.1000seo.com/website/940
or false,选中的情况下返回checked,未选中的情况返回undefined,看了jQuery文档后原来v1.6以后$(this).attr(‘checked’)就返回checked和undefined,v1.6以前返回true和false,v1.6以后可以使用$(this).is(‘:checked’)或者$(this).prop(‘checked’)来返回true和false
As of jQuery 1.6, the
.attr()method returns
undefinedfor
attributes that have not been set. In addition,
.attr()should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.
The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the
.attr()method
sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the
.prop()method
provides a way to explicitly retrieve property values, while
.attr()retrieves attributes.
For example,
selectedIndex,
tagName,
nodeName,
nodeType,
ownerDocument,
defaultChecked,
and
defaultSelectedshould be retrieved and set with the
.prop()method. Prior to jQuery 1.6, these properties were retrievable
with the
.attr()method, but this was not within the scope of
attr. These do not have corresponding attributes and are only
properties.
Concerning boolean attributes, consider a DOM element defined by the HTML markup
<input type="checkbox" checked="checked" />, and assume it is in a JavaScript variable named
elem:
elem.checked | true(Boolean) Will change with checkbox state |
---|---|
$(elem).prop("checked") | true(Boolean) Will change with checkbox state |
elem.getAttribute("checked") | "checked"(String) Initial state of the checkbox; does not change |
$(elem).attr("checked")(1.6) | "checked"(String) Initial state of the checkbox; does not change |
$(elem).attr("checked")(1.6.1+) | "checked"(String) Will change with checkbox state |
$(elem).attr("checked")(pre-1.6) | true(Boolean) Changed with checkbox state |
checkedattribute is a boolean attribute, which means the corresponding property is true if the attribute is present at all—even if, for example,
the attribute has no value or an empty string value. The preferred cross-browser-compatible way to determine if a checkbox is checked is to check for a “truthy” value on the element’s property using one of the following:
if ( elem.checked )
if ( $(elem).prop("checked") )
if ( $(elem).is(":checked") )
If using jQuery 1.6, the code
if ( $(elem).attr("checked") )will retrieve the actual content attribute, which does not change as the checkbox is checked and unchecked. It is meant only to store
the default or initial value of the checked property. To maintain backwards compatability, the
.attr()method in jQuery 1.6.1+ will retrieve and update the property for you so no code for boolean
attributes is required to be changed to
.prop(). Nevertheless, the preferred way to retrieve a checked value is with one of the options listed above.
转载自米豆网
本文链接地址: jQuery $(this).attr(‘checked’)出现undefined的问题
本文固定链接:http://www.1000seo.com/website/940
相关文章推荐
- jQuery $(this).attr(‘checked’)出现undefined的问题
- jQuery $(this).attr(‘checked’)出现undefined的问题
- jquery的$(this).attr("checked")一直返回undefined的问题
- 解决checkbox的attr(checked)一直为undefined问题
- 解决checkbox的attr(checked)一直为undefined问题
- 解决checkbox的attr(checked)一直为undefined问题
- checkbox的attr("checked")一直为undefined问题解决方案
- 解决checkbox的attr(checked)一直为undefined问题
- 解决checkbox的attr(“checked”)undefined问题
- 解决jquery升级1.6后checkbox的attr(checked)一直为undefined问题
- 解决checkbox的attr(checked)一直为undefined问题
- 解决checkbox的attr(checked)一直为undefined问题
- checkbox的attr(checked)一直为undefined问题
- $(this).attr("checked") 用jquery取checkbox的值 返回undefined
- $(this).attr(‘checked’)返回dundefined问题
- checkbox的attr(checked)一直为undefined问题
- 解决checkbox的attr(checked)一直为undefined问题
- checkbox的attr("checked")一直以来,undefined问题解决
- 解决checkbox的attr(checked)一直为undefined问题
- 解决JQuery checkbox的attr(checked)一直为undefined问题