【发布时间】:2015-04-16 02:11:17
【问题描述】:
我必须在同一页面上使用两种不同大小的复选框。我将根据复选框name 确定要使用的尺寸。如果name='room' 我将使用类'custom-checkbox' 的大图像。否则name='request',我会用custom-checkbox2班级的小图。
现在,我该如何修改下面的脚本,以便它可以根据复选框的名称来确定使用哪个类?
CSS:
.custom-checkbox{
width: 50px;
height: 50px;
display: inline-block;
position: relative;
z-index: 1;
top: 3px;
background: url("../img/big-check-nopass.png") no-repeat;
}
.custom-checkbox:hover{
background: url("../img/big-check-pass.png") no-repeat;
}
.custom-checkbox.selected{
background: url("../img/big-check-pass.png") no-repeat;
}
.custom-checkbox input[type="checkbox"]{
margin: 0;
position: absolute;
z-index: 2;
cursor: pointer;
outline: none;
opacity: 0;
/* CSS hacks for older browsers */
_noFocusLine: expression(this.hideFocus=true);
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: alpha(opacity=0);
-khtml-opacity: 0;
-moz-opacity: 0;
}
/* Let's Beautify Our Form */
label{
display: block;
padding: 2px 0;
}
/*for small cehckbox*/
.custom-checkbox2{
width: 20px;
height: 20px;
display: inline-block;
position: relative;
z-index: 1;
top: 3px;
background: url("../img/small-check-nopass.png") no-repeat;
}
.custom-checkbox2:hover{
background: url("../img/small-check-pass.png") no-repeat;
}
.custom-checkbox2.selected{
background: url("../img/small-check-pass.png") no-repeat;
}
.custom-checkbox2 input[type="checkbox"]{
margin: 0;
position: absolute;
z-index: 2;
cursor: pointer;
outline: none;
opacity: 0;
/* CSS hacks for older browsers */
_noFocusLine: expression(this.hideFocus=true);
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: alpha(opacity=0);
-khtml-opacity: 0;
-moz-opacity: 0;
}
/* Let's Beautify Our Form */
label2{
display: block;
padding: 2px 0;
}
Javascript:
function customCheckbox(checkboxName){
var checkBox = $('input[name="'+ checkboxName +'"]');
$(checkBox).each(function(){
$(this).wrap( "<span class='custom-checkbox'></span>" );
if($(this).is(':checked')){
$(this).parent().addClass("selected");
}
});
$(checkBox).click(function(){
$(this).parent().toggleClass("selected");
});
}
$(document).ready(function (){
customCheckbox("room[]");
})
HTML
<div class="row">
<div class="large-3 right columns">
<label><input type="checkbox" name="room[]" value="" /></label>
</div>
</div>
<div class="row">
<div class="large-3 right columns">
<label><input type="checkbox" name="request[]" value="" /></label>
</div>
</div>
编辑**
删除函数和修改脚本:
<script>
$(document).ready(function (){
$("input:checkbox").each(function(){
if($(this).attr("name") == "room[]") {
$(this).wrap( "<span class='custom-checkbox'></span>" );
if($(this).is(':checked')){
$(this).parent().addClass("selected");
}
} else if($(this).attr("name") == "request[]") {
$(this).wrap( "<span class='custom-checkbox2'></span>" );
if($(this).is(':checked')){
$(this).parent().addClass("selected");
}
}
});
})
</script>
【问题讨论】:
-
你能添加你的html代码吗?,如果你可以使用小提琴
-
你实际上可能不需要任何js,使用attrib css选择器:
[name='room']而不是.custom-checkbox -
@xzengga 我添加了 HTML 部分..你能帮忙吗?
-
@samantha: 好的,但你也可以使用
:checkedpsudo 选择器在 CSS 中应用它们。我只是认为使用 js 进行 CSS 可以处理的演示是错误的做法;我在上面看到的一切都可以用纯 CSS 完成 -
你能给他们不同的类名吗?
标签: javascript jquery css checkbox