首页 新闻 会员 周边

全选全不选的小案例,全不选效果出不来,没看出来问题在哪

0
[待解决问题]
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>反选全选</title>
		<script src="js/jquery-3.6.0.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/jquery-migrate-1.4.1.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/jquery-migrate-3.3.2.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		
		<form action="op.php">
			<p>选择爱好:</p>
			<p>
				<label for="">
					<input type="checkbox" name='dafa' value="dadfa"/>打篮球
				</label> 
				
				<label for="">
					<input type="checkbox" />踢足球
				</label> 
				<label for="">
					<input type="checkbox" />打羽毛球
				</label> 
				<label for="">
					<input type="checkbox" />打兵乓球
				</label> 
				<label for="">
					<input type="checkbox" />去游泳
				</label> 
			</p>
			
		</form>
		
		<p>
			<button id="all">全选</button>
		</p>
		<p>
			<button id="notall">全不选</button>
		</p>
		<p>
			<button id="unall">反选</button>
		</p>
		
		<script type="text/javascript">
			$('#all').click(function(){
				$(':checkbox').attr('checked',true);
			});
			
			/************ 全不选 不知道问题在哪?????*************/
			$('#notall').click(function(){
//				$(':checkbox').attr('checked',false);
				$(':checkbox').attr({'checked':false});
			});
			
			
			
			
			$('#unall').click(function(){
				$(':checkbox').each(function(){
					this.checked=!this.checked;
				});
			});
		</script>
	</body>
</html>

问题补充:

为什么 attr 这个方法不行,哪里出错了,其他两个就可以

牧_童的主页 牧_童 | 菜鸟二级 | 园豆:210
提问于:2021-08-08 10:13
< >
分享
所有回答(2)
0
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>反选全选</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  </head>
  <body>
    <form action="op.php">
      <p>选择爱好:</p>
      <p>
        <label for="">
          <input type="checkbox" name="dafa" value="dadfa" />打篮球
        </label>

        <label for=""> <input type="checkbox" />踢足球 </label>
        <label for=""> <input type="checkbox" />打羽毛球 </label>
        <label for=""> <input type="checkbox" />打兵乓球 </label>
        <label for=""> <input type="checkbox" />去游泳 </label>
      </p>
    </form>

    <p>
      <button id="all">全选</button>
    </p>
    <p>
      <button id="notall">全不选</button>
    </p>
    <p>
      <button id="unall">反选</button>
    </p>

    <script type="text/javascript">
      $('#all').click(function () {
        $(':checkbox').prop('checked', true)
      })

      $('#notall').click(function () {
        $(':checkbox').prop('checked', false)
      })

      $('#unall').click(function () {
        $(':checkbox').each(function () {
          this.checked = !this.checked
        })
      })
    </script>
  </body>
</html>
guangzan | 园豆:246 (菜鸟二级) | 2021-08-08 19:17
1

checked 是布尔属性,在 jQuery 里面布尔属性尽量使用 .prop(‘checked’, false) 方法来删除,
原生 DOM 就用 this.checked = false
.attr() 方法只能添加,不能删除,
删除用 .removeAttr('checked')

by.Genesis | 园豆:2719 (老鸟四级) | 2021-08-09 15:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册