【问题标题】:Check custom checkbox individually单独检查自定义复选框
【发布时间】:2016-08-31 08:35:19
【问题描述】:

大家好,提前感谢您的帮助!

我正在尝试为联系表单创建一些自定义复选框。默认复选框不可见,并且位于自定义复选框中,在示例中您将了解我在寻找什么。这个想法是,一旦您单击复选框,将显示一个自定义样式,表示该字段已被选中,如果您再次单击同一字段,该样式就会消失。现在我无法检查,因此样式,只有选定的复选框,而不是所有其他字段也被选中或取消选中。另一个问题是第一个字段中的复选框默认选中,我需要取消选中它们。

$('.checkbox input').on( 'click', function() {
    if( $(this).is(':checked') ){
        $('.checkbox').addClass('checked');
    } else {
        $('.checkbox').removeClass('checked');
    }
});
.checkbox{
	width: 125px;
	background-color: #fff;
	border: 2px solid #1AC4F8;
	display: inline-block;
	margin: 10px 0;
    font-size: 14px;
    color: gray;
    text-align: center;
    cursor: pointer;
    -webkit-transition: color 0.8s, background-color 0.8s;
	transition: color 0.8s, background-color 0.8s;
}

.checkbox:hover{
	color: white;
	background-color: #1AC4F8;
}

.checkbox input{
	opacity: 0;
	position: absolute;
	z-index: -1
}

.checkRow{
	text-align: center;	
}

.checked{
	color: white;
	background-color: #1AC4F8;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="checkRow">
                    <label class="checkbox">
                        <input type="checkbox" name="service" value="SEO">SEO
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="service" value="SEM">SEM
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="service" value="Emailing">Emailing
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="service" value="Social networks">Social networks
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="service" value="Web design">Web design
                    </label>
                </div>

我希望一切都清楚:)

【问题讨论】:

  • 非常感谢您的帮助!它是固定的:)

标签: jquery html css checkbox


【解决方案1】:

您遇到的主要问题是您在此处切换所有复选框...

$('.checkbox').addClass('checked');

这里

$('.checkbox').removeClass('checked');

您应该以这种方式引用正在(取消)检查的当前元素...

$(this).parent().addClass('checked');
$(this).parent().removeClass('checked');

$('.checkbox input').on('click', function() {
  if ($(this).is(':checked')) {
    $(this).parent().addClass('checked');
  } else {
    $(this).parent().removeClass('checked');
  }
});
.checkbox {
  width: 125px;
  background-color: #fff;
  border: 2px solid #1AC4F8;
  display: inline-block;
  margin: 10px 0;
  font-size: 14px;
  color: gray;
  text-align: center;
  cursor: pointer;
  -webkit-transition: color 0.8s, background-color 0.8s;
  transition: color 0.8s, background-color 0.8s;
}
.checkbox:hover {
  color: white;
  background-color: #1AC4F8;
}
.checkbox input {
  opacity: 0;
  position: absolute;
  z-index: -1
}
.checkRow {
  text-align: center;
}
.checked {
  color: white;
  background-color: #1AC4F8;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="checkRow">
  <label class="checkbox">
    <input type="checkbox" name="service" value="SEO">SEO
  </label>
  <label class="checkbox">
    <input type="checkbox" name="service" value="SEM">SEM
  </label>
  <label class="checkbox">
    <input type="checkbox" name="service" value="Emailing">Emailing
  </label>
  <label class="checkbox">
    <input type="checkbox" name="service" value="Social networks">Social networks
  </label>
  <label class="checkbox">
    <input type="checkbox" name="service" value="Web design">Web design
  </label>
</div>

【讨论】:

    【解决方案2】:

    我刚换了:

    • $('.checkbox').addClass('checked');$(this).parent().addClass('checked');
    • $('.checkbox').removeClass('checked');$(this).parent().removeClass('checked');

    我希望这就是你想要的:)

    $('.checkbox input').on( 'click', function() {
        if( $(this).is(':checked') ){
            $(this).parent().addClass('checked');
        } else {
            $(this).parent().removeClass('checked');
        }
    });
    .checkbox{
    	width: 125px;
    	background-color: #fff;
    	border: 2px solid #1AC4F8;
    	display: inline-block;
    	margin: 10px 0;
        font-size: 14px;
        color: gray;
        text-align: center;
        cursor: pointer;
        -webkit-transition: color 0.8s, background-color 0.8s;
    	transition: color 0.8s, background-color 0.8s;
    }
    
    .checkbox:hover{
    	color: white;
    	background-color: #1AC4F8;
    }
    
    .checkbox input{
    	opacity: 0;
    	position: absolute;
    	z-index: -1
    }
    
    .checkRow{
    	text-align: center;	
    }
    
    .checked{
    	color: white;
    	background-color: #1AC4F8;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div class="checkRow">
                        <label class="checkbox">
                            <input type="checkbox" name="service" value="SEO">SEO
                        </label>
                        <label class="checkbox">
                            <input type="checkbox" name="service" value="SEM">SEM
                        </label>
                        <label class="checkbox">
                            <input type="checkbox" name="service" value="Emailing">Emailing
                        </label>
                        <label class="checkbox">
                            <input type="checkbox" name="service" value="Social networks">Social networks
                        </label>
                        <label class="checkbox">
                            <input type="checkbox" name="service" value="Web design">Web design
                        </label>
                    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 2012-01-07
      • 2015-11-05
      • 2016-02-11
      • 2011-09-18
      • 2018-08-01
      • 1970-01-01
      相关资源
      最近更新 更多