为什么我的禁用/启用复选框在JQuery中不起作用?
问题描述:
我不明白为什么这段代码不起作用。请尝试在你的口水中具体,因为它一定是非常愚蠢的东西。为什么我的禁用/启用复选框在JQuery中不起作用?
$("#cagree").on("change", function(e){
\t \t if($("#chbx").attr("checked")){
\t \t $("#submitbtn").button("enable");
\t \t } else {
\t \t $("#submitbtn").button("disable");
\t \t }
\t \t
\t \t });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
\t <title>My Page</title>
\t <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div data-role="page">
\t <div data-role="content"> \t
\t \t <label><input type="checkbox" name="checkbox-0" id="chbx" class="cagree"> I agree </input></label>
<input type="button" value="Submit" id="submitbtn" class="submit" disabled/>
\t </div><!-- /content -->
</div><!-- /page -->
</body>
</html>
答
1. $("#cagree")
必须$(".cagree")
2.使用.is(":checked")
检查复选框被选中或不
的$("#submitbtn").button("enable")
3.Instead做$("#submitbtn").prop('disabled', false);
和副通用
工作片段: -
$(".cagree").on("change", function(e){ // it's class so use .
if($(this).is(":checked")){ // use $(this) for current-object
$("#submitbtn").prop('disabled', false); // it's property so use .prop
}else{
$("#submitbtn").prop('disabled', true);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div data-role="page">
<div data-role="content"> \t
<label><input type="checkbox" name="checkbox-0" id="chbx" class="cagree"> I agree </label>
<input type="button" value="Submit" id="submitbtn" class="submit" disabled/>
</div><!-- /content -->
</div><!-- /page -->
</body>
</html>
注: -
enable
和disable
的属性,以便使用.prop()
对其进行设置。
</input>
是无效的,所以删除。
可能重复[设置“选中”与jQuery复选框?](https://stackoverflow.com/questions/426258/setting-checked-for-a-checkbox-with-jquery) –
什么是$( “#submitbtn”)。button(“enable”)'应该这样做?也没有像''这样的标签 – j08691