Why my disable/enable checkbox is not working in JQuery?
61 观看
1回复
309 作者的声誉
I do not understan why this code is not working. Please, try to be specific in your anwer because it must be something really silly.
$("#cagree").on("change", function(e){
if($("#chbx").attr("checked")){
$("#submitbtn").button("enable");
} else {
$("#submitbtn").button("disable");
}
});
<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">
<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/>
</div><!-- /content -->
</div><!-- /page -->
</body>
</html>
回应 1
1像
60648 作者的声誉
1.$("#cagree")
need to be $(".cagree")
2.Use .is(":checked")
to check that checkbox is checked or not
3.Instead of $("#submitbtn").button("enable")
do $("#submitbtn").prop('disabled', false);
and vice-versa
Working snippet:-
$(".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">
<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>
Note:-
enable
and disable
are properties so use .prop()
to set them.
</input>
is invalid so remove that.
来自类别的问题 :
- jquery 使用JavaScript滚动溢出的DIV
- jquery 使用jQuery转义HTML字符串
- jquery 如何将html实体与jQuery进行比较
- jquery jQuery是否存在“存在”功能?
- jquery How do you remove all the options of a select box and then add one option and select it with jQuery?
- jquery 获取触发事件的元素的ID
- jquery 如何从JQuery中的模态对话框中获取结果
- jquery 删除所有以某个字符串开头的类
- jquery 如何在jQuery中停止效果
- jquery 如何显示jQuery中的加载微调器?
- jquery 有没有更好的方法来使用jQuery创建面向对象的类?
- jquery jQuery / JavaScript来替换破碎的图像
- jquery 用jQuery突出显示一个单词
- jquery jQuery获取textarea文本
- jquery 正则表达式匹配非英文字符?
- jquery 如何检测元素外部的单击?
- jquery 带有日期输入的jQuery Datepicker,不允许用户输入
- jquery 如何异步上传文件?
- jquery 使用jQuery获取表单输入字段?
- jquery 用jQuery作为JS对象向选项添加选项的最佳方法是什么?