jQuery中的attr()和prop()使用
总结:除了checked、seleted这样的属性推荐用prop()外,其他的随意都可以。
原因如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script type="text/javascript" src="../js/lib/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
function testCheck() {
var result1 = $("#check").prop("checked"); //结果是true
var result2 = $("#check").attr("checked"); //结果是checked
console.info("被选择的情况下" + "result1:" + result1 + "||result2:" + result2);
}
function testNoCheck() {
var result1 = $("#nocheck").prop("checked"); //结果是false
var result2 = $("#nocheck").attr("checked"); //结果是undefined
console.info("未有选择的情况下" + "result1:" + result1 + "||result2:" + result2);
} $(function () {
testCheck();//被选择的情况下result1:true||result2:checked
testNoCheck();//未有选择的情况下result1:false||result2:undefined
})
</script>
</head>
<body>
<input id="check" type="checkbox" value="football" checked="checked">足球
<input id="nocheck" type="checkbox" value="football">篮球
</body>
</html>
说明:
prop()对于seleted返回的是true或者false,更直观。
jQuery中的attr()和prop()使用的更多相关文章
- jquery中的attr与prop的区别,什么时候用attr,什么时候用prop
只要有 Boolean() 属性的,简单说就是具有true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),(其实这些都是表单类的), ...
- jquery中的attr与prop
http://www.cnblogs.com/Showshare/p/different-between-attr-and-prop.html
- jQuery知识点:attr与prop的区别
做项目时遇到个莫名的问题,全选的时候仅第一次有效,再次点击全选按钮是无效了,查了查原因,看到篇很不错的文章,问题出在jquery中的attr属性上,这里做下笔记. 原文链接:http://www.cn ...
- jQuery属性选择器.attr()和.prop()两种方法
在判断表单单选框是否被选中时,通常会想到使用$('#checkbox').attr('checked')来判断,但在一些情况下,你会发现这种方法并不管用,得到的是undefined. 原来jQuery ...
- jquery checkbox反复调用attr('checked', true/false)只有第一次生效 Jquery 中 $('obj').attr('checked',true)失效的几种解决方案
1.$('obj').prop('checked',true) 2. $(':checkbox').each(function(){ this.checked=true; }) 为什么:attr为失效 ...
- jQuery中使用attribute,prop获取,设置input的checked值【转】
1.prop方法获取.设置checked属性 当input控件checkbox设置了checked属性时,无论checked=”“或 checked=”checked”,$(obj).prop(“ch ...
- jQuery中使用attribute,prop获取,设置input的checked值
1.prop方法获取.设置checked属性 当input控件checkbox设置了checked属性时,无论checked=”“或 checked=”checked”,$(obj).prop(“ch ...
- 【jQuery 区别】attr()和prop()的区别
1>>> 今天实现一个 点击更新按钮 ,可以勾选上本行的的checkbox的功能: 使用代码: /** * updateproduct.htmls 更新 产品信息 */ $(docu ...
- jquery中的attr()与prop()的区别
根据官方的建议:具有 true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),其他的使用 attr()
随机推荐
- laravel多种安装方法
首先请确保环境为 PHP >= 5.5.9 OpenSSL PHP 扩展 PDO PHP 扩展 Mbstring PHP 扩展 Tokenizer PHP 扩展 方法一: 直接下载安装好的lar ...
- hh monitor
http://theholyjava.wordpress.com/2012/09/21/enabling-jmx-monitoring-for-hadoop-and-hive/ http://blog ...
- byte数组如何转为short数组 (转)
最近在搞毕业设计,做的是有关语音识别的手机应用.在处理音频的过程中,发现需要用short数组处理音频,可能光用byte会越界.但是java读文件没有一次性读到short数组中的api,要么是一个一个读 ...
- VS2010中安装AjaxControlToolkit
原文地址:http://www.asp.net/ajaxlibrary/act.ashx 第一步 下载Ajax Control Toolkit 进入网址http://ajaxcontroltoolki ...
- java中对List中对象排序实现
package com.test; import java.util.ArrayList; import java.util.Collections; import java.util.Compara ...
- 导出Excel数据
先要导入jxl架包,其中的abc.xls为测试Excel,具体代码如下,仅供参考: import java.io.File; import java.io.FileInputStream; impor ...
- python3 与 python2的 区别比较
http://sebug.net/paper/books/dive-into-python3/porting-code-to-python-3-with-2to3.html
- 给input元素添加float. 去除IE6 下input的空隙
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Node.js学习 - Route
Node.js 路由 URL解析,需要url和querystring两个模块url.parse(string).query | url.parse(string).pathname | | | | | ...
- ubuntu11.10server 安装php-redis插件
1.下载php-redis插件 1 sudo wget https://github.com/nicolasff/phpredis/archive/master.zip 2.安装 1 2 3 4 5 ...