热门关键字:
jquery > jquery教程 > jquery教程 > 如何使用jQuery动态地选中或取消选中复选框

如何使用jQuery动态地选中或取消选中复选框

413
作者:管理员
发布时间:2021/2/5 13:36:05
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=3870
答:使用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
友荐云推荐