选中的复选框只能运行一次

时间:2019-05-23 11:00:23

标签: javascript jquery checkbox

邮件发送表单,客户必须至少选择一种选择(邮件,SMS)。

如果未选择发送按钮,则将其禁用;如果同时选择其中一个或两个,则将其激活。

$(document).ready(function() {
  $('.form-check-input').change(function() {
    $(this).each(function() {
      if (!$(this).is(':checked')) {
        $("#send").attr("disabled", "disabled");
      }
    });
  });
});
<div>
  <div class="row">
    <div class="col-md-12 form-check">
      <div>
        <input class="form-check-input" type="checkbox" value="" id="Epost" checked>
        <label class="form-check-label" for="E-post">E-post</label>
      </div>
      <div>
        <input class="form-check-input" type="checkbox" value="" id="SMS">
        <label class="form-check-label" for="SMS">SMS</label>
      </div>
      <br>
      <button id='send'>Send</button>
    </div>
  </div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

5 个答案:

答案 0 :(得分:4)

问题是因为您当前的逻辑不处理被重新选中的复选框。

您可以通过反转逻辑来解决此问题,以使disabled属性在每次复选框更改时都更新,并由复选框的length确定:

var $checkboxes = $('.form-check-input').change(function() {
  $('#send').prop('disabled', function() {
    return $checkboxes.filter(':checked').length == 0;
  });
});
<div>
  <div class="row">
    <div class="col-md-12 form-check">
      <div>
        <input class="form-check-input" type="checkbox" value="" id="Epost" checked>
        <label class="form-check-label" for="E-post">E-post</label>
      </div>
      <div>
        <input class="form-check-input" type="checkbox" value="" id="SMS">
        <label class="form-check-label" for="SMS">SMS</label>
      </div>
      <br>
      <button id='send'>Send</button>
    </div>
  </div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:2)

您可以这样做:

$('.form-check-input').change(function() {
  $("#send").prop("disabled", ($('.form-check-input:checked').length == 0 ? true : false));
});

如果未选中任何复选框,则将禁用该按钮。

演示

$(document).ready(function() {
  $('.form-check-input').change(function() {
    $("#send").prop("disabled", ($('.form-check-input:checked').length == 0 ? true : false));
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <div class="row">
    <div class="col-md-12 form-check">
      <div>
        <input class="form-check-input" type="checkbox" value="" id="Epost" checked>
        <label class="form-check-label" for="E-post">
                        E-post
                    </label>
      </div>
      <div>
        <input class="form-check-input" type="checkbox" value="" id="SMS">
        <label class="form-check-label" for="SMS">
                        SMS
                    </label>
      </div>
      <br>
      <button id='send'>Send</button>
    </div>
  </div>
</div>

答案 2 :(得分:0)

您再也不会启用该按钮。只有一条禁用指令。

默认情况下,我会在一开始将其禁用,如果选中任何复选框,则将其启用。

$(document).ready(function() {
  $('.form-check-input').change(function() {
    $("#send").attr("disabled", "disabled");
    $(this).each(function() {
      if ($(this).is(':checked')) {
        $("#send").removeAttr("disabled");
      }
    });
  });
});
<div>
  <div class="row">
    <div class="col-md-12 form-check">
      <div>
        <input class="form-check-input" type="checkbox" value="" id="Epost" checked>
        <label class="form-check-label" for="E-post">E-post</label>
      </div>
      <div>
        <input class="form-check-input" type="checkbox" value="" id="SMS">
        <label class="form-check-label" for="SMS">SMS</label>
      </div>
      <br>
      <button id='send'>Send</button>
    </div>
  </div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 3 :(得分:0)

$(document).ready(function() {
  $('.form-check-input').change(function() {
    let hasChecked = false;
    $('.form-check-input').each(function() {
      if ($(this).is(':checked')) {
        hasChecked = true;
      }
    });
    $("#send").attr("disabled", !hasChecked);
  });
});
<div>
  <div class="row">
    <div class="col-md-12 form-check">
      <div>
        <input class="form-check-input" type="checkbox" value="" id="Epost" checked>
        <label class="form-check-label" for="E-post">E-post</label>
      </div>
      <div>
        <input class="form-check-input" type="checkbox" value="" id="SMS">
        <label class="form-check-label" for="SMS">SMS</label>
      </div>
      <br>
      <button id='send'>Send</button>
    </div>
  </div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 4 :(得分:0)

更正您的逻辑。禁用后,选中后将不再启用。

$(document).ready(function() {
  $('.form-check-input').change(function() {
    $(this).each(function() {
      if (!$(this).is(':checked')) {
        $("#send").attr("disabled", true);
      }else{
        $("#send").attr("disabled", false);
      }
    });
  });
});

Blockquote