以下是我的代码。当我选择复选框时,文本不会改变。 复选框的文本应该改变,但不是。选择复选框文本不变
<html>
<head>
<title>OX</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
$('#checkbox1').on('change', function() {
window.alert(5 + 6);
if ($('#checkbox1').is(':checked')) {
window.alert(5 + 6);
$("#description").html("Your Checked 1");
} else {
$("#description").html("No check");
}
});
</script>
</head>
<body>
<input type="checkbox" value="0" id="checkbox1" name=""/> Answer one <br/></input>
<span id="description"> Hi Your Text will come here </span>
</body>
</html>
你正在运行的代码之前存在的元素。查看$(document).ready,以便在dom准备就绪之前不会触发代码https://learn.jquery.com/using-jquery-core/document-ready/ –
或..移动您的JS代码BELOW HTML – Zak
另外,[''](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input)是一个[“空元素”](https://开发者。 mozilla.org/en-US/docs/Glossary/empty_element),这意味着它不能包含任何子节点。我建议使用['