如果选中了多个复选框,如何添加事件
How can I add event if multple checkboxes are checked?
https://jsfiddle.net/rsu1cjss/7/
我要显示div(仅此一个)
<div class="two-colors box">You have selected <strong>two colors</strong> so i am here</div>
如果检查了红色和绿色
JS-
$('input[type="checkbox"]').click(function(){
if($(this).attr("value")=="red"){
$(".red").toggle();
}
if($(this).attr("value")=="green"){
$(".green").toggle();
}
if($(this).attr("value")=="blue"){
$(".blue").toggle();
}
if($(this).attr("value")=="yellow"){
$(".yellow").toggle();
}
});
HTML
<div>
<label><input type="checkbox" name="colorCheckbox" value="red"> red</label>
<label><input type="checkbox" name="colorCheckbox" value="green"> green</label>
<label><input type="checkbox" name="colorCheckbox" value="blue"> blue</label>
<label><input type="checkbox" name="colorCheckbox" value="yellow"> yellow</label>
</div>
<div class="red box">You have selected <strong>red checkbox</strong> so i am here</div>
<div class="green box">You have selected <strong>green checkbox</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue checkbox</strong> so i am here</div>
<div class="yellow box">You have selected <strong>blue checkbox</strong> so i am here</div>
<div class="two-colors box">You have selected <strong>two colors</strong> so i am here</div>
CSS
.box{
padding: 20px;
display: none;
margin-top: 20px;
border: 1px solid #000;
}
.red{ background: #ff0000; }
.green{ background: #00ff00; }
.blue{ background: #0000ff; }
.yellow{ background: yellow; }
.two-colors {background: pink}
响应点击,选中红色和绿色复选框上的checked
属性,只有当它们都为true时才显示div:
$('input[type="checkbox"]').click(function(){
var red = $("input[type=checkbox][value=red]")[0];
var green = $("input[type=checkbox][value=green]")[0];
$(".two-colors.box").toggle(red.checked && green.checked);
// ...
});
请注意,该代码中的单个div条件也错误:复选框的value
为始终为"blue"
、"yellow"
等。;更改的是是否选中复选框。因此,您可以使用值(this.value
)查找要显示的div,并使用checked
属性查找toggle
:的参数
$("." + this.value).toggle(this.checked);
因此:
$('input[type="checkbox"]').click(function(){
// Handle the combined one
var red = $("input[type=checkbox][value=red]")[0];
var green = $("input[type=checkbox][value=green]")[0];
$(".two-colors.box").toggle(red.checked && green.checked);
// Handle the singles:
$("." + this.value).toggle(this.checked);
});
(请注意,对于input
元素,通常$(this).attr("value")
在检查值时是不正确的;使用$(this).val()
或仅使用this.value
,因为在这种情况下我们不需要jQuery。value
属性和value
特性对于input
元素来说是不一样的。)
实时示例:
$('input[type="checkbox"]').click(function() {
// Handle the combined one
var red = $("input[type=checkbox][value=red]")[0];
var green = $("input[type=checkbox][value=green]")[0];
$(".two-colors.box").toggle(red.checked && green.checked);
// Handle the singles:
$("." + this.value).toggle(this.checked);
});
.box {
padding: 20px;
display: none;
margin-top: 20px;
border: 1px solid #000;
}
.red {
background: #ff0000;
}
.green {
background: #00ff00;
}
.blue {
background: #0000ff;
}
.yellow {
background: yellow;
}
.two-colors {
background: pink
}
<div>
<label>
<input type="checkbox" name="colorCheckbox" value="red">red</label>
<label>
<input type="checkbox" name="colorCheckbox" value="green">green</label>
<label>
<input type="checkbox" name="colorCheckbox" value="blue">blue</label>
<label>
<input type="checkbox" name="colorCheckbox" value="yellow">yellow</label>
</div>
<div class="red box">You have selected <strong>red checkbox</strong> so i am here</div>
<div class="green box">You have selected <strong>green checkbox</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue checkbox</strong> so i am here</div>
<div class="yellow box">You have selected <strong>blue checkbox</strong> so i am here</div>
<div class="two-colors box">You have selected <strong>two colors</strong> so i am here</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
或者使用更多jQuery:;-)
$('input[type="checkbox"]').click(function(){
// Handle the combined one
var redChecked = $("input[type=checkbox][value=red]").is(":checked");
var greenChecked = $("input[type=checkbox][value=green]").is(":checked");
$(".two-colors.box").toggle(redChecked && greenChecked);
// Handle the singles:
var $this = $(this);
$("." + $this.val()).toggle($this.is(":checked"));
});
$('input[type="checkbox"]').click(function(){
// Handle the combined one
var redChecked = $("input[type=checkbox][value=red]").is(":checked");
var greenChecked = $("input[type=checkbox][value=green]").is(":checked");
$(".two-colors.box").toggle(redChecked && greenChecked);
// Handle the singles:
var $this = $(this);
$("." + $this.val()).toggle($this.is(":checked"));
});
.box {
padding: 20px;
display: none;
margin-top: 20px;
border: 1px solid #000;
}
.red {
background: #ff0000;
}
.green {
background: #00ff00;
}
.blue {
background: #0000ff;
}
.yellow {
background: yellow;
}
.two-colors {
background: pink
}
<div>
<label>
<input type="checkbox" name="colorCheckbox" value="red">red</label>
<label>
<input type="checkbox" name="colorCheckbox" value="green">green</label>
<label>
<input type="checkbox" name="colorCheckbox" value="blue">blue</label>
<label>
<input type="checkbox" name="colorCheckbox" value="yellow">yellow</label>
</div>
<div class="red box">You have selected <strong>red checkbox</strong> so i am here</div>
<div class="green box">You have selected <strong>green checkbox</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue checkbox</strong> so i am here</div>
<div class="yellow box">You have selected <strong>blue checkbox</strong> so i am here</div>
<div class="two-colors box">You have selected <strong>two colors</strong> so i am here</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
请注意,这假设只有值为red
和green
的复选框才是您关心的复选框。如果您需要进一步缩小范围,请给它们一个类或ID。
相关文章:
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在映射数组中添加换行符
- 正在将数据主题添加到所有项目
- ZeroClipboard-在复制之前添加到值
- 我可以在json对象中添加一个函数吗
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何添加浮动和非浮动,其他
- 添加文字和评论功能更新Div
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 在函数中添加数组元素的数值
- 如何将歌曲添加到jPlayer
- JavaScript/jQuery-添加添加和删除类与下一个函数之间的延迟
- wooccommerce登录页:如何添加添加到购物车按钮
- 如何添加添加边框的无限制点击
- 我如何添加添加到一个javascript对象
- 得到我的tabpanel组件,但不能使用添加添加一个选项卡
- 如何在yuidoc中添加添加外部链接(grunt contrib yuidoc)
- 在队列jQuery中添加添加/删除类点击图片