attr('checked',false)只能使用一次 或者初始,但是jquery1.6版本后有prop()可以反复使用 js也可以
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
function check() {
$('input').attr('checked',true)
}
function uncheck() {
$('input').attr('checked',false)
}
</script>
</head>
<body>
Checkbox: <input type="checkbox" id="myCheck">
<button onclick="check()">Check Checkbox</button>
<button onclick="uncheck()">Uncheck Checkbox</button>
</body>
</html>
<body>
<p><input type="checkbox" name="vehicle" value="Bike" /> I have a bike</p>
<button class="button0">xzczxczc</button>
<button class="button1">xzczxczc</button>
<script>
$('input:checkbox').each(function () {
$(this).prop('checked',false);
});
$('.button0').click(function () {
$('input:checkbox').prop('checked',true);
})
$('.button1').click(function () {
alert($('input:checkbox').prop('checked'))
$('input:checkbox').prop('checked',false);
})
</script>
</body>
</html>
ss
<body>
Checkbox: <input type="checkbox" id="myCheck">
<button onclick="check()">Check Checkbox</button>
<button onclick="uncheck()">Uncheck Checkbox</button>
<script>
function check() {
document.getElementById("myCheck").checked = true;
}
function uncheck() {
document.getElementById("myCheck").checked = false;
}
</script>
</body>