全选、全不选、反选

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body {
width: 300px;
height: 170px;
border: 1px solid #c0d9ff;
margin: auto;
}

#title, #state {
margin-left: 5px;
width: 280px;
height: 30px;
border: 1px solid #c0d9ff;
}

#name {
width: 280px;
height: 30px;
border: 1px solid #c0d9ff;
padding-top: 10px;
margin-top: 10px;
margin-left: 5px;
}
</style>
</head>
<body>
<p id="title">问卷调查:你最喜欢的歌手是谁?</p>
<div id="state">
<!-- name相同时,按钮不会同时为选中状态-->
<input class="selectAll" type="radio" name="0">全选
<input class="selectNone" type="radio" name="0">全不选
<input class="invertSelect" type="radio" name="0">反选
</div>
<div id="name">
<input type="checkbox">刘德华
<input type="checkbox">张学友
<input type="checkbox">孙燕姿
<input type="checkbox">刘欢
</div>
<script src="adapter/jquery-3.4.1.js" type="text/javascript"></script>
<script>
//全选
$(function () {
$(".selectAll").on("click", function () {
$(":checkbox").prop("checked", true);
});
//全不选
$(".selectNone").on("click", function () {
$(":checkbox").prop("checked", false);
});

//取反
$(".invertSelect").on("click", function () {
// :checkbox 是type中的选择器,选择全部 type为checkbox的属性
$(":checkbox").each(function(){
        //遍历的时候,里面的对象是DOM对象,this是DOM对象
        //写法一
        //
$(this).prop("checked",!$(this).prop("checked"))
          //写发二:
                $(this).prop("checked",!this.checked);
             

})
});
})
</script>
</body>
</html>

 

 

转载于:https://www.cnblogs.com/taoist123/p/11165825.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是HTML中实现全选全不选反选的代码示例: 1. 全选效果 ```html <!DOCTYPE html> <html> <head> <title>全选</title> <script type="text/javascript"> function selectAll(){ var checkboxes = document.getElementsByName("fruit"); for(var i=0; i<checkboxes.length; i++){ checkboxes[i].checked = true; } } </script> </head> <body> <h3>水果列表</h3> <form> <input type="checkbox" name="fruit" value="apple">苹果<br> <input type="checkbox" name="fruit" value="banana">香蕉<br> <input type="checkbox" name="fruit" value="orange">橙子<br> <input type="checkbox" name="fruit" value="pear">梨子<br> <input type="button" value="全选" onclick="selectAll()"> </form> </body> </html> ``` 2. 全不选效果 ```html <!DOCTYPE html> <html> <head> <title>全不选</title> <script type="text/javascript"> function unselectAll(){ var checkboxes = document.getElementsByName("fruit"); for(var i=0; i<checkboxes.length; i++){ checkboxes[i].checked = false; } } </script> </head> <body> <h3>水果列表</h3> <form> <input type="checkbox" name="fruit" value="apple">苹果<br> <input type="checkbox" name="fruit" value="banana">香蕉<br> <input type="checkbox" name="fruit" value="orange">橙子<br> <input type="checkbox" name="fruit" value="pear">梨子<br> <input type="button" value="全不选" onclick="unselectAll()"> </form> </body> </html> ``` 3. 反选效果 ```html <!DOCTYPE html> <html> <head> <title>反选</title> <script type="text/javascript"> function reverseSelect(){ var checkboxes = document.getElementsByName("fruit"); for(var i=0; i<checkboxes.length; i++){ checkboxes[i].checked = !checkboxes[i].checked; } } </script> </head> <body> <h3>水果列表</h3> <form> <input type="checkbox" name="fruit" value="apple">苹果<br> <input type="checkbox" name="fruit" value="banana">香蕉<br> <input type="checkbox" name="fruit" value="orange">橙子<br> <input type="checkbox" name="fruit" value="pear">梨子<br> <input type="button" value="反选" onclick="reverseSelect()"> </form> </body> </html> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值