最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to prevent unchecking all checkboxes using jquery? - Stack Overflow

programmeradmin5浏览0评论

I have custom checkboxes:

<p>
    <input class="preventUncheck" id="toggle-on1" type="checkbox">
    <label for="toggle-on1">Selling</label>
</p>
<p>
    <input class="preventUncheck" id="toggle-on2" type="checkbox">           
    <label for="toggle-on2">Rent</label> 
</p>
$(function() {
    //prevent to uncheck all checkboxes
    $('.preventUncheck').on('change', function() {
        $('.preventUncheck').length == 0
        && !this.checked
        && $(this).prop('checked', true);
    });
});

I want to prevent unchecking all checkbox, that means alway one checkbox should be checked and if user try to unchecking it, javascript prevent to do it.

How can i do this work?

I have custom checkboxes:

<p>
    <input class="preventUncheck" id="toggle-on1" type="checkbox">
    <label for="toggle-on1">Selling</label>
</p>
<p>
    <input class="preventUncheck" id="toggle-on2" type="checkbox">           
    <label for="toggle-on2">Rent</label> 
</p>
$(function() {
    //prevent to uncheck all checkboxes
    $('.preventUncheck').on('change', function() {
        $('.preventUncheck').length == 0
        && !this.checked
        && $(this).prop('checked', true);
    });
});

I want to prevent unchecking all checkbox, that means alway one checkbox should be checked and if user try to unchecking it, javascript prevent to do it.

How can i do this work?

Share Improve this question edited Feb 2, 2017 at 11:46 Mohammad 21.5k16 gold badges57 silver badges85 bronze badges asked Feb 2, 2017 at 11:26 uzhasuzhas 9254 gold badges15 silver badges28 bronze badges 2
  • Your code is by default both are checked, and if you uncheck it, it should check again or not ? – Rahul Commented Feb 2, 2017 at 11:37
  • In short, what you want to be more precise – Rahul Commented Feb 2, 2017 at 11:38
Add a ment  | 

3 Answers 3

Reset to default 7

You should check if length of checked checkbox is equal to 0 then prevent to unchecking it.

$('.preventUncheck').on('change', function(e) {
    if ($('.preventUncheck:checked').length == 0 && !this.checked)
    	this.checked = true;
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
  <input type="checkbox" class="preventUncheck" id="toggle-on1">
  <label for="toggle-on1">Selling</label>
</p>
<p>
  <input type="checkbox" class="preventUncheck" id="toggle-on2">
  <label for="toggle-on2">Rent</label> 
</p>

Add attr "disabled" to the checkbox?

You could also prevent unchecking of the last checked checkbox by disabling it as long as it is the sole checked one.

$('#demo').on('change', '.preventUncheck', function () {
    var $checkedEls = $('#demo .preventUncheck:checked');
    $checkedEls.prop('disabled', $checkedEls.length === 1);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="demo">
<p>
  <input type="checkbox" class="preventUncheck" id="toggle-on1" checked>
  <label for="toggle-on1">Sell</label>
</p>
<p>
  <input type="checkbox" class="preventUncheck" id="toggle-on2">
  <label for="toggle-on2">Rent</label> 
</p>
<p>
  <input type="checkbox" class="preventUncheck" id="toggle-on3" checked>
  <label for="toggle-on3">Lease</label> 
</p>
<p>
  <input type="checkbox" class="preventUncheck" id="toggle-on4">
  <label for="toggle-on4">Lend</label> 
</p>
</div>

(While at it, I also changed it so that only a single event handler is registered.)

发布评论

评论列表(0)

  1. 暂无评论