【问题标题】:How do I manipulate the same function in javascript for two different classes如何在 javascript 中为两个不同的类操作相同的函数
【发布时间】: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: 好的,但你也可以使用:checked psudo 选择器在 CSS 中应用它们。我只是认为使用 js 进行 CSS 可以处理的演示是错误的做法;我在上面看到的一切都可以用纯 CSS 完成
  • 你能给他们不同的类名吗?

标签: javascript jquery css checkbox


【解决方案1】:

您可以使用“每个”jQuery 函数,该函数将检查页面上复选框类型的每个输入。所以,你很容易得到它的名字,如果它是“room[]”,将custom-checkbox-big-size类添加到span wrap,如果它的名字是“request[]”,将custom-checkbox-small-size类添加到span wrap(我改变了你的 CSS 让事情变得简单)。

JS:

var wrapSpan = $('<span class="custom-checkbox-span"</span>');
$('input:checkbox').each(function() {
    if ($(this).is(':checked')) {
        wrapSpanspan.addClass('checked');
    }
    if($(this).attr("name") == "room[]") {
        wrapSpan.addClass("custom-checkbox-big-size");
    }
    if($(this).attr("name") == "request[]") {
        wrapSpan.addClass("custom-checkbox-small-size");
    }
    $(this).wrap(wrapSpan).hide();
});

$(".custom-checkbox-span").on("mouseup",function(){
    checkSpan($(this));
});

function checkSpan(el) {
    if(el.hasClass("selected")) {
        el.removeClass("selected");
        el.children().prop("checked",false);
    } else {
        el.addClass("selected");
        el.children().prop("checked",true);
    }
}

CSS:

.custom-checkbox-big-size {
    background:red;
    width: 50px;
    height: 50px;
    display: inline-block;
}
.custom-checkbox-big-size:hover, .custom-checkbox-big-size.selected {
    background:yellow;
}
.custom-checkbox-small-size {
    background:blue;
    width: 20px;
    height: 20px;
    display: inline-block;
}
.custom-checkbox-small-size:hover, .custom-checkbox-small-size.selected {
    background:green;
}

检查jsFiddle。我是用颜色做的,因为我没有你的原始图片,你必须改变background: color;规则。

【讨论】:

  • 当我检查你的小提琴时,这正是我想要的,但我不确定如何将它与我的功能集成。正如您提到的,我尝试将它放在最后一个括号之后,但它没有做任何更改。你能帮忙吗?
  • 我在 jsFiddle 中添加了更多代码。如果您的函数只是从复选框中切换所选的类,那将按您的预期完成工作。但是,如果您需要跨度,请告诉我,我会再次更新。
  • 现在我放置了你以前的小提琴版本脚本。它显示大或小复选框取决于我想要的复选框的名称。但该功能不起作用。该函数假设在复选框的状态下显示不同的背景图像。
  • 但是你想要背景在哪个元素中?在复选框内部还是外部?还是特定元素,还是全身背景?
  • 该功能将根据 css 将复选框替换为正常、悬停和选中状态的图像。
【解决方案2】:

这是在选择器中处理的,假设您使用的是 CSS3,就像这样......

input[type*='checkbox'][name*='room'] {
    ... styles here
}

【讨论】:

  • 添加 * 用于子字符串匹配 do 的事实是 name 属性是一个数组
  • 我很欣赏这一点,但我更喜欢使用 js,因为它可以正常工作。只需要修改它,以便它可以根据复选框名称指定使用哪个类。你能帮忙吗?
  • 然后你应该能够用类似于上面的选择器替换函数内的第一行,在等号之前添加 * 字符。例如: var checkBox = $('input[name*="'+ checkboxName +'"]');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-06
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 2020-11-24
相关资源
最近更新 更多