🔖js
2021-05-29 10:57阅读: 15评论: 0推荐: 0

js复选框之全选&&反选&&不选

今日分享:纯js复选框之全选&&反选&&不选

想要了解其用法,首先先得了解它们各自的意思

全选:全部都选中

反选:选中的部分取消,没选中的部分勾选上

不选:全部取消勾选

了解大概意思后,需要了解复选框的一点就是,选中时,复选框的值为true;取消勾选时,复选框的值为false。再是checked属性的使用,根据这就可以解决是否可以选中了。

好了直接上代码。


js代码:

            //全选
            function allSelect(){
				var res  = document.getElementsByName("aihao");
				for(var i=0; i< res.length;i++){
					res[i].checked = true; 
				}
			}
            //反选
			function reSelect(){
				var res = document.getElementsByName("aihao");
				for(var i=0;i<res.length;i++){
					if(res[i].checked == true){
						res[i].checked = false;
					}else{
						res[i].checked = true;
					}
					
				}
			}
			//不选
			function notAllSelect(){
				var res  = document.getElementsByName("aihao");
				for(var i=0; i< res.length;i++){
					res[i].checked = false; 
				}
			}

html代码:

        <input type="checkbox" name="aihao" id="basketball" />篮球
		<input type="checkbox" name="aihao" id="soccer"  />足球
		<input type="checkbox" name="aihao" id="tennis" />网球
		<input type="checkbox" name="aihao" id="badminton"  />乒乓球
		<input type="checkbox" name="aihao" id="pingpong"  />羽毛球
		<input type="checkbox" name="aihao" id="volleyball"  />排球
		<br />
		<input type="button" name="" id="" value="全选" onclick="allSelect()"/>
		<input type="button" name="" id="" value="反选" onclick="reSelect()" />
		<input type="button" name="" id="" value="不选" onclick="notAllSelect()" />

总结一下,这其中js部分运用了循环遍历、函数、获取相应的元素集合等。主要需要了解复选框的是否选中时的值以及checked属性的用法。

 

posted @   所遇所思  阅读(15)  评论(0编辑  收藏  举报  
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
🔑
点击右上角即可分享
微信分享提示
  1. 1 404 not found REOL
  2. 2 偏爱 张芸京
  3. 3 Glimpse of Us Joji
Glimpse of Us - Joji
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Connor McDonough/Alexis Kesselman/Castle/Riley McDonough/George Miller

作曲 : Connor McDonough/Alexis Kesselman/Castle/Riley McDonough/George Miller

She’d take the world off my shoulders

She’d take the world off my shoulders

If it was ever hard to move

She’d turn the rain to a rainbow

When I was living in the blue

Why then if she is so perfect

Do I still wish that it was you

Perfect don’t mean that it’s working

So what can I do

When you’re out of sight

When you’re out of sight

In my mind

Cause sometimes

Cause sometimes

I look in her eyes and

That’s where I find

A glimpse of us

And I try

To fall for her touch

But I’m thinking of

The way it was

Said I’m fine

And said I moved on

I’m only here passing time in her arms

Hoping I’ll find

A glimpse of us

Tell me he savors your glory

Tell me he savors your glory

Does he laugh the way I did

Is this a part of your story

One that I had never lived

Maybe one day you’ll feel lonely

And in his eyes you’ll get a glimpse

Maybe you’ll start slipping slowly

And find me again

When you’re out of sight

When you’re out of sight

In my mind

Cause sometimes

Cause sometimes

I look in her eyes and

That’s where I find

A glimpse of us

And I try

To fall for her touch

But I’m thinking of

The way it was

Said I’m fine

And said I moved on

I’m only here passing time in her arms

Hoping I’ll find

A glimpse of us

Oooh

Oooh

Oooh-ooh-ooh

Oooh

Oooh-oooh-oooh-ooh-ooh

Cause sometimes

Cause sometimes

I look in her eyes and

That’s where I find

A glimpse of us

And I try

To fall for her touch

But I’m thinking of

The way it was

Said I’m fine

And said I moved on

I’m only here passing time in her arms

Hoping I’ll find

A glimpse of us