您的位置:首页 > 其它

复选框checkbox 判断是否选中及添加移除选中状态

2018-02-01 10:48 429 查看
<!DOCTYPE html>
<htmllang="en">

<head>
<metacharset="UTF-8">
<metaname="viewport"content="width=device-width, initial-scale=1.0">
<metahttp-equiv="X-UA-Compatible"content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="checkbox"id="checkbox">
<button id="button">点我看是否选中</button>
<br>
<br>
<input type="checkbox"id="checkbox2">
<button id="button2">点我切换选中状态</button>
<script src="./jquery.min.js"></script>
<script>
$('#button').click(function () {
if ($('#checkbox').is(':checked')) {
console.log('选中')
} else {
console.log('没有选中')
}
})
// 第二种方法
$('#button').click(function () {
if ($('#checkbox').prop('checked')) {
console.log('选中prop')
} else {
console.log('没有选中prop')
}
})
// jquery1.6 +: prop的4种赋值:
// $("#checkbox2cb1″).prop("checked",true);
// $("#checkbox2).prop({checked:true}); //map键值对
// $("#checkbox2).prop("checked",function(){ })
// $("#checkbox2).prop("checked","checked");
var onOff
= true;
$('#button2').click(function (){
if (onOff) {
$("#checkbox2").prop("checked",true);
} else {
$("#checkbox2").prop("checked",false);
}
onOff = !onOff;
})
</script>
</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息