信息:jQuery 1.6.0 HTML复选框:
首先,我确保jQuery加载:
if(jQuery) {
alert("loaded");
}
Run Code Online (Sandbox Code Playgroud)
就是这样,它已经装好了.然后在我使用之后立即行:
var returnvalue = jQuery("#mycheckbox").attr("checked");
alert(returnvalue);
Run Code Online (Sandbox Code Playgroud)
由于某种原因,这始终警告'未定义'.我100%确定ID存在.我也试过以下方法:
jQuery("#mycheckbox").find('input[type="checkbox"]').each(function(){
alert("foo");
if (jQuery(this).is(":checked"))
ReturnVal = true;
});
Run Code Online (Sandbox Code Playgroud)
但这甚至没有弹出警报.
我究竟做错了什么?我到目前为止是一个jQuery或Javascript专家,但这应该工作......对吗?
编辑:我试过了:
jQuery(document).ready(function() {
alert(id);
var returnvalue = jQuery("#" . id).prop("checked");
alert(returnvalue);
});
Run Code Online (Sandbox Code Playgroud)
alert(id)为我提供了正确的ID,但alert(returnvalue)返回undefined.
我也尝试过:
if(jQuery) {
alert("loaded");
}
jQuery(function() {
alert(id);
var returnvalue = jQuery("#" . id).attr("checked");
alert(returnvalue);
});
Run Code Online (Sandbox Code Playgroud)
结果相同
Edit2:使用.prop后,用"+"而不是"."附加id.(PHP习惯),它的工作原理.感谢大家的帮助!
Rig*_*red 16
1.6或更高版本,请使用prop()而不是attr():var returnvalue = jQuery("#mycheckbox").prop("checked");
Run Code Online (Sandbox Code Playgroud)
这是因为如果您没有checked在元素中包含显式属性,那么属性是undefined属性将具有其默认值.
.find():jQuery("#mycheckbox").each(function(){
alert("foo");
if (jQuery(this).is(":checked"))
ReturnVal = true;
});
Run Code Online (Sandbox Code Playgroud)
在问题中包含您的HTML会很有帮助.
| 归档时间: |
|
| 查看次数: |
25560 次 |
| 最近记录: |