jQuery复选框事件处理程序Bug



我最近发现了一个问题,我似乎不能弄清楚关于jQuery和复选框事件处理

应该发生的是:

  • 单击与特定复选框相关联的框图像。
  • 当box-image被标记为checked(通过添加一个.checked类来表示)并显示右上角复选标记指示器)相关的

当我重复/快速点击灰色的方框图像瓦片时,你会看到有问题的bug。默认行为(切换选中的属性)似乎是在事件处理程序触发后随机发生的。因此,这将导致关联的选定框-图像和复选框随机地彼此不同步,从而给出不准确的数据。

请到https://jsfiddle.net/coreyds/5wLk2rge/4/

查看此示例并执行错误操作。

你有一个解决方案来解决这个问题,无论是在jQuery或纯JavaScript?

下面是问题代码:

HTML:

<div class="container">
<div class="row">
    <div class="col-sm-6">
        <div class="form-group hwl-inline-checkbox-group">
            <label class="checkbox-inline">
                <div id="sa-checkbox-img1" class="sa-checkbox-img">
                    <i class="fa fa-check"></i>
                    <i class="fa fa-briefcase fa-3x"></i>
                    <label class="icon-img-label">WORK</label>
                </div>
                <input type="checkbox" id="imgCheck1" name="surrounding_area" class="sa-checkbox" value="work" />
            </label>
            <label class="checkbox-inline">
                <div id="sa-checkbox-img2" class="sa-checkbox-img">
                    <i class="fa fa-check"></i>
                    <i class="fa fa-building fa-3x"></i>
                    <label class="icon-img-label">SCHOOL</label>
                </div>
                <input type="checkbox" id="imgCheck2" name="surrounding_area" class="sa-checkbox" value="school" />
            </label>
            <label class="checkbox-inline">
                <div id="sa-checkbox-img3" class="sa-checkbox-img">
                    <i class="fa fa-check"></i>
                    <i class="fa fa-meh-o fa-3x"></i>
                    <label class="icon-img-label">NO PREFERENCE</label>
                </div>
                <input type="checkbox" id="imgCheck3" name="surrounding_area" class="sa-checkbox" value="no preference" />
            </label>
        </div>
    </div>
</div>
<!-- end .row -->

jQuery

$('.sa-checkbox-img').click(function(){
    var $this = $(this),
        sa_checkbox = $this.find($('.sa-checkbox'));
    if( !$this.hasClass('checked')){
        $this.addClass('checked');
        sa_checkbox.prop('checked', true);
    } else {
        $this.removeClass('checked');
        sa_checkbox.prop('checked', false);
    }
});
CSS

.container,
.row {
    margin-top: 20px;
}
h5 {
    text-align: center;
}
.sa-checkbox-img {
  color: #999;
  border: 5px solid #999;
  padding: 10px 25px;
  text-align: center;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  -moz-transition-duration: 250ms;
  -o-transition-duration: 250ms;
  -webkit-transition-duration: 250ms;
  transition-duration: 250ms;
  -moz-transition-property: all;
  -o-transition-property: all;
  -webkit-transition-property: all;
  transition-property: all;
}
.sa-checkbox-img label.icon-img-label {
  display: block;
}
.sa-checkbox-img i.fa.fa-check {
  display: none;
  color: white;
}
.sa-checkbox-img:hover {
  border-color: #009fd4;
  color: #009fd4;
  cursor: default;
}
.sa-checkbox-img.checked {
  border-color: #009fd4;
  color: #009fd4;
  cursor: default;
}
.sa-checkbox-img.checked i.fa.fa-check {
  display: block;
  background: #009fd4;
  position: absolute;
  top: -5px;
  right: -5px;
  color: white;
  padding: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  border-radius: 5px;
}

为了更好地结合" label - input "对,需要使用" for="someId" - id="someId "对。你的错误是在检查.sa-checkbox-img块上的click,当你需要检查.sa-checkbox 上的click。因为一直以来,当你点击一些与复选框绑定的内部标签时,会触发该复选框上的click事件,但点击复选框不会触发标签上的click事件。

工作示例

祝你好运!

哇,这个bug很难破解,直到我意识到你代码中的小bug !我不知道发生了什么,但我终于解决了。你可以在这里找到解决方案。我也能简化你的逻辑。主要的变化是标签元素。您忘记了for属性!

<label for="imgCheck1" class="icon-img-label">SCHOOL</label>

相关内容

最新更新