答:使用jQueryprop()方法
您可以使用jQueryprop()方法动态地选中或取消选中复选框,例如单击按钮或超链接等。该prop()方法需要jQuery 1.6及更高版本。
让我们尝试以下示例以了解其基本工作原理:
例试试这个代码»
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Check/Uncheck Checkbox</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$(".check").click(function(){
$("#myCheck").prop("checked", true);
});
$(".uncheck").click(function(){
$("#myCheck").prop("checked", false);
});
});
</script>
</head>
<body>
<p><input type="checkbox" id="myCheck"> Are you sure?</p>
<button type="button" class="check">Yes</button>
<button type="button" class="uncheck">No</button>
</body>
</html>
如果您觉得本文的内容对您的学习有所帮助:
关键字:
jQuery