设置可以选择的复选框数量限制

时间:2017-02-03 18:23:48

标签: javascript jquery function limit

我在这里搜索过其他类似的问题并尝试复制它们,但我不确定我做错了什么。

我只想要一个复选框才能被检查。我用limitCal设置限制并检查兄弟姐妹。

任何人都能看到我做错了什么?

jQuery.fn.fadeBoolToggle = function (bool) {
        return bool ? this.fadeIn(400) : this.fadeOut(400);
    }    
    function packageSelect() {
      var limitCal = 1;
        $('.calendar-check').on('change', function () {
            $(this).parents('.product-wrap:first').find('.checkmark-img').fadeBoolToggle(this.checked);
            if ($(this).siblings(':checked').length >= limitCal) {
                this.checked = false;
            }
            $('#next1').fadeBoolToggle($('.product-check:checked').length > 0);
            var prods = [];
            $('.calendar-check:checked').each(function () { prods.push($(this).val()) });
        });
    };
    packageSelect();
.calendar-check {
  display: none;
}
.product-wrap {
    width: 100%;
    position: relative;
    display: block;
}
.checkmark-img {
    display: none;
    width: 40%;
    height: auto;
    z-index: 1;
    cursor: pointer;
}
.package-check-toggle {
  position: relative;
  height: 100%;
  width: 100%;
  display: block;
}
.package-setup {
    display: none;
    margin: 40px 0;
    width: 100%;
}

#calendar-box-wrap {
    margin: 20px 0;
}
.calendar-box {
    display: inline-block;
    vertical-align: top;
    width: 25%;
    margin: 0 4%;
    position: relative;
}
.calendar-selection-img {
    width: 100%;
    height: auto;
    cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="calendar-box">
                            <div class="product-wrap">
                                <label for="cal-2year" class="package-check-toggle">
                                    <img src="images/calendar-package.png" alt="2-year Photo Gift" class="calendar-selection-img">
                                    <img src="images/checkmark-circle.png" class="checkmark-img total-center">
                                </label>
                                <input type="checkbox" class="calendar-check" id="cal-2year" value="2-year Calendar">
                            </div>
                        </div><div class="calendar-box">
                            <div class="product-wrap">
                                <label for="cal-whiteboard" class="package-check-toggle">
                                    <img src="images/calendar-package.png" alt="Whiteboard Photo Gift" class="calendar-selection-img">
                                    <img src="images/checkmark-circle.png" class="checkmark-img total-center">
                                </label>
                                <input type="checkbox" class="calendar-check" id="cal-whiteboard" value="Whiteboard Calendar">
                            </div>
                        </div>

2 个答案:

答案 0 :(得分:3)

这是您尝试做的简化版本。基本上,拦截复选框的click事件可以阻止默认操作(选中复选框)。 checked this的值将是点击成功时的值。因此,如果未选中该复选框,请点击该按钮。如果已选中复选框的数量小于或等于限制,也会发生单击。否则,请停止选中此复选框。我已将此示例的限制设置为2,仅用于演示。

&#13;
&#13;
var limit = 2;
$('input:checkbox').on('click', function (e) {
  if (!this.checked || $('input:checkbox:checked').length <= limit) {
    return true;
  }
  return false;
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
&#13;
&#13;
&#13;

答案 1 :(得分:0)

&#13;
&#13;
const allCheckBoxs = document.querySelectorAll("input")
let limit = 2
allCheckBoxs.forEach( el => {
  
  el.addEventListener("click", e => {
    if(el.checked){
      limit--
      
      if(limit < 0){
        e.preventDefault();
        console.log("Sorry, you are over the limit")
      }
      
    }else{
      limit++
      console.log(limit)
    }
    
  })
  
})
&#13;
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
&#13;
&#13;
&#13;