我尝试更改按钮上的disabled
属性,以防复选框:
<input type="checkbox" id="terms" class="styled" name="terms">Accept <a href="#">terms of service</a>
将检查,应删除disabled
属性,否则它应保留在按钮元素上。
我按照以下方式工作:
if(document.getElementById('terms').checked) {
$("#registerButton").removeAttr('disabled');
}else{
$("#registerButton").attr('disabled','disabled');
}
可悲的是,这只会工作一次。检查完复选框后,按钮将不会更改。是否有这样的功能是onchange编辑按钮?
答案 0 :(得分:2)
你可以这样做。
$('#terms').change(function() {
$("#registerButton").prop('disabled', !this.checked);
}).change();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="terms" class="styled" name="terms">Accept <a href="#">terms of service</a>
<button id="registerButton">Register</button>
&#13;
答案 1 :(得分:1)
在javascript中:
摘录:
document.getElementById('terms').addEventListener('change', function(e) {
document.getElementById('registerButton').disabled = !this.checked;
});
document.getElementById('registerButton').disabled = true;
&#13;
<input type="checkbox" id="terms" class="styled" name="terms">Accept <a href="#">terms of service</a>
<button type="button" id="registerButton">registerButton</button>
&#13;
答案 2 :(得分:0)
<html>
<head></head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style>
</style>
<body>
<input type="checkbox" id="terms" class="styled" name="terms">Accept <a href="#">terms of service</a>
<button id="mybutton" disabled>Click on me</button>
<script type="text/javascript">
$("#terms").on('change',function(){
var check_checkbox = $(this).prop('checked');//check the checkbox status
if(check_checkbox == true)//if it is checked
{
$("#mybutton").prop('disabled',false)
}
else//if it is not checked
{
$("#mybutton").prop('disabled',true);
}
})
</script>
</body>
</html>
&#13;