菜鸟问题:jquery无法设置checkbox选中 jQueryCheckBoxJavaScript 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <html> <meta content="text/html;charset=shift_jis"/> <head> <script type="text/javascript" src="./jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("input").attr("checked","checked");}); </script></head> <body> test: <input type="checkbox"/><br/></body></html>这样就可以的啊 用这个:$("input[type='checkbox']").prop("checked",true); $(":checkbox").attr("checked",true) 恩恩,楼上的都ok,貌似你写的也是ok的要不行就用dom,document.getElementById("id").checked = true; $("input").prop("checked",true);ttributes和properties之间的差异在特定情况下是很重要。jQuery 1.6之前 ,.attr()方法在取某些 attribute 的值时,会返回 property 的值,这就导致了结果的不一致。从 jQuery 1.6 开始, .prop()方法 方法返回 property 的值,而 .attr() 方法返回 attributes 的值。例如, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, 和 defaultSelected 应使用.prop()方法进行取值或赋值。 在jQuery1.6之前,这些属性使用.attr()方法取得,但是这并不是元素的attr属性。他们没有相应的属性(attributes),只有特性(property)。例如,考虑一个DOM元素的HTML标记中定义的<input type="checkbox" checked="checked" /> ,并假设它是一个JavaScript变量命名的elem :elem.checked true (Boolean) 将改变复选框的状态$(elem).prop("checked") true (Boolean) 将改变复选框的状态elem.getAttribute("checked") "checked" (String) 不会改变的复选框的初始状态;$(elem).attr("checked") (1.6) "checked" (String) 不会改变的复选框的初始状态;$(elem).attr("checked") (1.6.1+) "checked" (String) 将改变复选框的状态$(elem).attr("checked") (pre-1.6) true (Boolean) 将改变复选框的状态根据W3C的表单规范 ,在checked属性是一个布尔属性,这意味着只要该 attribute 存在,即使它没有值,或是一个空字符串,该属性对应的 property 就是 true。以下推荐的是兼容浏览器方式,判断 checkbox 元素的 checked 属性是否为"真" 的方法:if ( elem.checked )if ( $(elem).prop("checked") )if ( $(elem).is(":checked") )如果你使用jQuery 1.6 ,代码if ( $(elem).attr("checked") ),将获得一个属性(attribute) ,它不改变该复选框被选中和选中。它只是用来存储默认或选中属性的初始值。为了保持向后兼容,.attr() 方法从 jQuery 1.6.1+ 开始除了返回属性值外,还会更新 property 属性,因此 boolean attribute(布尔属性)不需要通过 .prop() 来改变其值。推荐使用上述方法之一,来取得 checked 的值。 例如,考虑一个DOM元素的HTML标记中定义的<input type="checkbox" checked="checked" /> ,并假设它是一个JavaScript变量命名的elem :elem.checked true (Boolean) 将改变复选框的状态$(elem).prop("checked") true (Boolean) 将改变复选框的状态elem.getAttribute("checked") "checked" (String) 不会改变的复选框的初始状态;$(elem).attr("checked") (1.6) "checked" (String) 不会改变的复选框的初始状态;$(elem).attr("checked") (1.6.1+) "checked" (String) 将改变复选框的状态$(elem).attr("checked") (pre-1.6) true (Boolean) 将改变复选框的状态 更新一下,这个情况是出现在Chrome浏览器下的,在IE中是完全没问题的 我这个就是在chrome下面测试的,没有这样的问题啊 我这个就是在chrome下面测试的,没有这样的问题啊我的也是在chrome下测试的,这个问题就是有时候会出现,纠结 我这个就是在chrome下面测试的,没有这样的问题啊我的也是在chrome下测试的,这个问题就是有时候会出现,纠结我这个就是在chrome下面测试的,没有这样的问题啊我的也是在chrome下测试的,这个问题就是有时候会出现,纠结chrome下没任何问题呀! http://api.jquery.com/prop/网页中有详细介绍。我在IE10和chrome中选中后,用attr不能改变checkbox的状态,换成prop就可以了。jQuery版本是1.10.1 <head>$(function(){ $(":checkbox").attr("checked",true);})</head> attr是set/getAttribute的jQuery版,prop是this.XXX/this.XXXX=XX的jQuery版。我一般保险的用这个,不用关心jQuery版本和浏览器问题$("input")[0].checked = true;if($("#checkAll")[0].checked) { $(".checkIt").each(function(){this.checked = true;})}老生长谈的问题,归根结底是这俩的区别:checkboxElement.setAttribute('checked','checked');checkboxElement.checked = true;类似的还有:inputElement.value, optionElement.selected, inputElement.disabled....如果某个版本的jQ在某个浏览器下用attr("checked","checked")能生效,那只能说明jQ做了特殊的treat,对已经存在于DOM中的checkbox来说,其UI表现(UI上选中与否)也即真实值,与checkboxElement.checked这个布尔值是时时对应更新的,那个Attribute只在浏览器第一次渲染这个控件的时候作为初始值之用,setAttribute无法时时更新UI(真实值)。论attribute和property(属性和属性,中文居然也匮乏了)。参见:http://www.w3help.org/zh-cn/causes/SD9006 我也遇到了,1.10用prop处理$("input").prop({checked:true}); 这种方式 document.getElementById("id").checked = true; 可行 delete删除 变量 document.write 不用正则如何验证文本框里的值只能包含字母数字或下划线 请问以下两段代码是否编码过有关联的?? 初学者之《JS》&《AJAX》书籍评测! hwkj.jquery.chromatable.js锁定表头竖线对齐问题 请教这样正则表达式如何写 窗体一回发js代码执行不了??? xmlHttp.responseText中的数据在js中是乱码,请问该如何解决? for 循环问题,大虾进 jQuery datagrid 列错位应该如何解决?? com组件如何异步调用js函数?
<meta content="text/html;charset=shift_jis"/>
<head>
<script type="text/javascript" src="./jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("input").attr("checked","checked");
}); </script>
</head>
<body>
test: <input type="checkbox"/><br/></body>
</html>
这样就可以的啊
要不行就用dom,document.getElementById("id").checked = true;
$("input").prop("checked",true);
ttributes和properties之间的差异在特定情况下是很重要。jQuery 1.6之前 ,.attr()方法在取某些 attribute 的值时,会返回 property 的值,这就导致了结果的不一致。从 jQuery 1.6 开始, .prop()方法 方法返回 property 的值,而 .attr() 方法返回 attributes 的值。例如, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, 和 defaultSelected 应使用.prop()方法进行取值或赋值。 在jQuery1.6之前,这些属性使用.attr()方法取得,但是这并不是元素的attr属性。他们没有相应的属性(attributes),只有特性(property)。例如,考虑一个DOM元素的HTML标记中定义的<input type="checkbox" checked="checked" /> ,并假设它是一个JavaScript变量命名的elem :elem.checked true (Boolean) 将改变复选框的状态
$(elem).prop("checked") true (Boolean) 将改变复选框的状态
elem.getAttribute("checked") "checked" (String) 不会改变的复选框的初始状态;
$(elem).attr("checked") (1.6) "checked" (String) 不会改变的复选框的初始状态;
$(elem).attr("checked") (1.6.1+) "checked" (String) 将改变复选框的状态
$(elem).attr("checked") (pre-1.6) true (Boolean) 将改变复选框的状态
根据W3C的表单规范 ,在checked属性是一个布尔属性,这意味着只要该 attribute 存在,即使它没有值,或是一个空字符串,该属性对应的 property 就是 true。以下推荐的是兼容浏览器方式,判断 checkbox 元素的 checked 属性是否为"真" 的方法:if ( elem.checked )
if ( $(elem).prop("checked") )
if ( $(elem).is(":checked") )
如果你使用jQuery 1.6 ,代码if ( $(elem).attr("checked") ),将获得一个属性(attribute) ,它不改变该复选框被选中和选中。它只是用来存储默认或选中属性的初始值。为了保持向后兼容,.attr() 方法从 jQuery 1.6.1+ 开始除了返回属性值外,还会更新 property 属性,因此 boolean attribute(布尔属性)不需要通过 .prop() 来改变其值。推荐使用上述方法之一,来取得 checked 的值。
例如,考虑一个DOM元素的HTML标记中定义的<input type="checkbox" checked="checked" /> ,并假设它是一个JavaScript变量命名的elem :
elem.checked true (Boolean) 将改变复选框的状态
$(elem).prop("checked") true (Boolean) 将改变复选框的状态
elem.getAttribute("checked") "checked" (String) 不会改变的复选框的初始状态;
$(elem).attr("checked") (1.6) "checked" (String) 不会改变的复选框的初始状态;
$(elem).attr("checked") (1.6.1+) "checked" (String) 将改变复选框的状态
$(elem).attr("checked") (pre-1.6) true (Boolean) 将改变复选框的状态
我这个就是在chrome下面测试的,没有这样的问题啊
我这个就是在chrome下面测试的,没有这样的问题啊我的也是在chrome下测试的,这个问题就是有时候会出现,纠结
我这个就是在chrome下面测试的,没有这样的问题啊我的也是在chrome下测试的,这个问题就是有时候会出现,纠结
我这个就是在chrome下面测试的,没有这样的问题啊我的也是在chrome下测试的,这个问题就是有时候会出现,纠结chrome下没任何问题呀!
网页中有详细介绍。我在IE10和chrome中选中后,用attr不能改变checkbox的状态,换成prop就可以了。jQuery版本是1.10.1
$(function(){
$(":checkbox").attr("checked",true);
})
</head>
if($("#checkAll")[0].checked) {
$(".checkIt").each(function(){this.checked = true;})
}老生长谈的问题,归根结底是这俩的区别:checkboxElement.setAttribute('checked','checked');
checkboxElement.checked = true;
类似的还有:inputElement.value, optionElement.selected, inputElement.disabled....如果某个版本的jQ在某个浏览器下用attr("checked","checked")能生效,那只能说明jQ做了特殊的treat,对已经存在于DOM中的checkbox来说,其UI表现(UI上选中与否)也即真实值,与checkboxElement.checked这个布尔值是时时对应更新的,那个Attribute只在浏览器第一次渲染这个控件的时候作为初始值之用,setAttribute无法时时更新UI(真实值)。论attribute和property(属性和属性,中文居然也匮乏了)。
参见:http://www.w3help.org/zh-cn/causes/SD9006
$("input").prop({checked:true});