如何为第一次选择和第二次选择复选框应用不同的颜色
how to apply different colors for first select and second select checkbox
我想为第一个和第二个复选框应用不同的颜色,第一个复选框应该是绿色,第二个应该是红色。如果用户取消选中绿色,下一个应该是绿色。如果用户未选中红色,则表示下一个复选框应为红色。请帮帮我。
<script class="jsbin" src="jquery.min.js"></script>
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
<body>
<form name="disc">
<p>Select Only Two</p>
<input type="checkbox" id="ad"/>
<input type="checkbox" id="ai"/>
<input type="checkbox" id="as"/>
<input type="checkbox" id="ac"/>
</form>
<script>
$("form").on("click", ":checkbox", function(event){
$(":checkbox:not(:checked)", this.form).prop("disabled", function(){
return $(this.form).find(":checkbox:checked").length == 2;
});
});
</script>
试试这个JSFiddle演示
JS:
$(function() {
$('input[type=checkbox]').each(function() {
var span = $('<span class="' + $(this).attr('type') + ' ' + $(this).attr('class') + '"></span>').click(doCheck).mousedown(doDown).mouseup(doUp);
if ($(this).is(':checked')) {
span.addClass('checked');
}
$(this).wrap(span).hide();
});
function doCheck() {
if ($(this).hasClass('checked')) {
if($('#disc').find(":checkbox:checked").length >= 1) {
$(this).removeClass('checked');
$(this).removeClass('red');
$(this).removeClass('green');
$(this).addClass('default');
$(this).children().prop("checked", false);
}
} else {
console.log($('#disc').find(":checkbox:checked").length);
if($('#disc').find(":checkbox:checked").length < 2) {
$(this).addClass('checked');
$(this).removeClass('default');
if($('#disc').find(":checkbox:checked").length < 1) {
$(this).addClass('green');
} else {
$(this).addClass('red');
}
$(this).children().prop("checked", true);
}
}
}
function doDown() {
$(this).addClass('clicked');
}
function doUp() {
$(this).removeClass('clicked');
}
});
CSS:
.checkbox, .radio {
width: 19px;
height: 20px;
padding: 0px;
background: url("http://i48.tinypic.com/raz13m.jpg");
display: block;
clear: left;
float: left;
}
.checked {
background-position: 0px -50px;
}
.clicked {
background-position: 0px -25px;
}
.clicked.checked {
background-position: 0px -75px;
}
.green {
background-color: green;
}
.red {
background-color: red;
}
.default {
background-color: none;
}
希望这就是你想要的。
附言:这是我对CSS样式复选框中接受的fiddle所做的一个轻微修改,因此背景着色归功于原作者Jeff B
相关文章:
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 如何将返回的值应用于多个不同位置的多个选择器
- 谷歌表单-根据分配给谷歌应用程序的工作为商业用户预先填充多项选择列表
- 如何选择当前元素以应用悬停效果?没有继承
- 选择具有ID的父级的第一个子级以外的所有子级,然后应用操作
- 使用peta poco ORM在MVC应用程序中创建选择列表
- 在应用 jQuery.Selected 时,使用 jQuery.Validate 来验证选择字段时,暴露错误
- 如何在点击链接后从用户取消对话框中获得回调以选择移动应用程序
- Ember.在使用路由器的应用中选择内容绑定
- 在HTML5、CSS、JavaScript中添加选项选择器,类似于移动应用程序中的选项设置
- 如何在Sencha Touch应用程序的选择字段内自定义列表
- JQuery插件选择不在我的Ionic应用程序中工作
- 在同构的React web应用程序中,选择服务器渲染的最佳方式是什么
- 此api版本不支持onedrive文件选择器aadsts70001应用程序
- jQuery日期选择器未显示在我的应用程序中
- 在AngularJS中构建SPA应用程序与纯JavaScript - 选择哪种Boostrap进行样式设置
- 选择框在应用 css/javascript 时失去功能
- 如何使用 Dropbox 插入式应用始终从“我的帐户”中选择文件
- 如果所选范围为空,则在光标处应用“选择”
- 选择应用程序各个页面的元素