添加课程的问题(Issue adding a class)

编程入门 行业动态 更新时间:2024-10-22 04:54:08
添加课程的问题(Issue adding a class)

我正在尝试在检查输入时添加一个类。 但是,我的尝试不起作用。 该课程没有添加。 我尝试了addClass和toggleClass 。

有谁看到我做错了什么?

 $('.social-input').on('change', function () {
		var social1 = $('#social1');
		var social2 = $('#social2');
		if (social1.is(':checked')) {
			$(this).addClass('active');
		}
		else if (social2.is(':checked')) {
			$(this).toggleClass('active');
			
		}
	}); 
  
.social-option-box {
	display: inline-block;
	border: 1px solid #45ba95;
	padding: 20px 0;
	margin: 20px 5%;
	width: 35%;
	text-align: center;
	font-size: 1.2em;
	color: #45ba95;
	cursor: pointer;
	transition: ease-in-out .5s;
}
.social-option-box.active {
	transition: ease-in-out .5s;
	background: #45ba95;
	color: #FFF;
}
.social-option-box:hover {
	background-color: #45ba95;
	color: #FFF;
	transition: ease-in-out .5s;
}
.social-input {
	display: none;
} 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label class="social-option-box" for="social1">Yes, following
	<input type="radio" name="social" id="social1" class="social-input" value="Yes, following">
</label>
<label class="social-option-box" for="social2">No, I'm not
	<input type="radio" name="social" id="social2" class="social-input" value="Not following">
</label> 
  
 

I am attempting to add a class when an input is checked. However, my attempt is not working. The class does not add. I tried addClass and toggleClass.

Does anyone see what I am doing wrong?

 $('.social-input').on('change', function () {
		var social1 = $('#social1');
		var social2 = $('#social2');
		if (social1.is(':checked')) {
			$(this).addClass('active');
		}
		else if (social2.is(':checked')) {
			$(this).toggleClass('active');
			
		}
	}); 
  
.social-option-box {
	display: inline-block;
	border: 1px solid #45ba95;
	padding: 20px 0;
	margin: 20px 5%;
	width: 35%;
	text-align: center;
	font-size: 1.2em;
	color: #45ba95;
	cursor: pointer;
	transition: ease-in-out .5s;
}
.social-option-box.active {
	transition: ease-in-out .5s;
	background: #45ba95;
	color: #FFF;
}
.social-option-box:hover {
	background-color: #45ba95;
	color: #FFF;
	transition: ease-in-out .5s;
}
.social-input {
	display: none;
} 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label class="social-option-box" for="social1">Yes, following
	<input type="radio" name="social" id="social1" class="social-input" value="Yes, following">
</label>
<label class="social-option-box" for="social2">No, I'm not
	<input type="radio" name="social" id="social2" class="social-input" value="Not following">
</label> 
  
 

最满意答案

除了其他正确的答案,您可以通过执行以下JS来简化这一点。

 $('.social-input').on('change', function () {
       var box = $('.social-option-box');
       var checkedBox = box.has('.social-input:checked');
       box.not(checkedBox).removeClass('active');
       checkedBox.addClass('active');
 }); 
  
.social-option-box {
	display: inline-block;
	border: 1px solid #45ba95;
	padding: 20px 0;
	margin: 20px 5%;
	width: 35%;
	text-align: center;
	font-size: 1.2em;
	color: #45ba95;
	cursor: pointer;
	transition: ease-in-out .5s;
}
.social-option-box.active {
	transition: ease-in-out .5s;
	background: #45ba95;
	color: #FFF;
}
.social-option-box:hover {
	background-color: #45ba95;
	color: #FFF;
	transition: ease-in-out .5s;
}
.social-input {
	display: none;
} 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label class="social-option-box" for="social1">Yes, following
	<input type="radio" name="social" id="social1" class="social-input" value="Yes, following">
</label>
<label class="social-option-box" for="social2">No, I'm not
	<input type="radio" name="social" id="social2" class="social-input" value="Not following">
</label> 
  
 

Along with the other correct answers, you may be able to simplify this a little by doing the following JS.

 $('.social-input').on('change', function () {
       var box = $('.social-option-box');
       var checkedBox = box.has('.social-input:checked');
       box.not(checkedBox).removeClass('active');
       checkedBox.addClass('active');
 }); 
  
.social-option-box {
	display: inline-block;
	border: 1px solid #45ba95;
	padding: 20px 0;
	margin: 20px 5%;
	width: 35%;
	text-align: center;
	font-size: 1.2em;
	color: #45ba95;
	cursor: pointer;
	transition: ease-in-out .5s;
}
.social-option-box.active {
	transition: ease-in-out .5s;
	background: #45ba95;
	color: #FFF;
}
.social-option-box:hover {
	background-color: #45ba95;
	color: #FFF;
	transition: ease-in-out .5s;
}
.social-input {
	display: none;
} 
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label class="social-option-box" for="social1">Yes, following
	<input type="radio" name="social" id="social1" class="social-input" value="Yes, following">
</label>
<label class="social-option-box" for="social2">No, I'm not
	<input type="radio" name="social" id="social2" class="social-input" value="Not following">
</label> 
  
 

更多推荐

本文发布于:2023-07-24 00:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:课程   Issue   class   adding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!