我有这样的代码
<div class="checkbox">
<input type="checkbox" id="checkme" value ="accept"/>
<label>I have read and agree to the terms and conditions</label>
<p><input type="submit" name="submit" value="Order now!" id="sub1" disabled="disabled"/></p>
我试图把这个Jscript放在代码下面:
<script>
$(document).ready(function() {
var the_terms = $("#checkme");
the_terms.click(function() {
if ($(this).is(":checked")) {
$("#sub1").removeAttr("disabled");
} else {
$("#sub1").attr("disabled", "disabled");
}
});
});
</script>
然而,它根本不起作用。我已经遵循了互联网上的所有指南。有人能帮上忙吗?我做错了什么?除了这些,还有其他代码吗?"
哦,这是php格式的
编辑:也这么做了
<script>
var checker = document.getElementById('checkme');
var sendbtn = document.getElementById('sub1');
checker.onchange = function() {
sendbtn.disabled = !!this.checked;
};
</script>
但是,如果未选中,如何更改为禁用?
只需像这样使用jquery更改事件:
$(document).ready(function() {
$('#checkme').change(function() {
if ($(this).is(":checked")) {
$("#sub1").removeAttr("disabled");
} else {
$("#sub1").attr("disabled", "disabled");
}
});
});
$(function() {
$('#id_of_your_checkbox').click(function() {
if ($(this).is(':checked')) {
$('#id_of_your_button').attr('disabled', 'disabled');
} else {
$('#id_of_your_button').removeAttr('disabled');
}
});
});