简体   繁体   English

是否根据复选框启用和禁用按钮

[英]Button enabled and disabled based on checkbox is checked or not

I have been facing an issue when none checkbox is checked then button will not be disabled it's still enabled. 当未选中任何复选框时,我一直遇到一个问题,即该按钮仍处于启用状态,因此不会被禁用。

I also put attribute on button of disabled but still it's enabled; 我还将属性设置为禁用按钮,但仍然启用;

here is my code 这是我的代码

<input class="form-check-input tab1_chk" type="checkbox" name="2[]"  value="1">Option 1

<input class="form-check-input tab1_chk" type="checkbox" name="2[]"  value="2">Option 2

<input class="form-check-input tab1_chk" type="checkbox" name="2[]"  value="2">Option 3
<br>
<br>
<button type='button' class='tab1_btn' name="next" id="next">              
    Next       
</button>
if ($(".tab1_chk:checked" )) {
  $('.tab1_btn').prop('disabled', false);
} else {
  $('.tab1_btn').prop('disabled', true);
}

When I give the alert before this $('.tab1_btn').prop('disabled', false); 当我在此$('.tab1_btn').prop('disabled', false);之前发出警报时$('.tab1_btn').prop('disabled', false);

Then alert is showing when it's run but also giving alert 2 before this $('.tab1_btn').prop('disabled', true); 然后将显示警报何时运行,同时在$('.tab1_btn').prop('disabled', true);之前给出警报2 $('.tab1_btn').prop('disabled', true); then alert is not showing only first alert is showing 然后警报不显示仅显示第一个警报

JSFiddle 的jsfiddle

I need when no any checkbox is checked then next button will be disabled 我需要在未选中任何复选框的情况下禁用下一个按钮

You need to apply an event handler 您需要应用事件处理程序

$('.tab1_btn').prop('disabled', !$('.tab1_chk:checked').length);//initially disable/enable button based on checked length
$('input[type=checkbox]').click(function() {
    if ($('.tab1_chk:checkbox:checked').length > 0) {
        $('.tab1_btn').prop('disabled', false);
    } else {
        $('.tab1_btn').prop('disabled', true);
    }
});

Working snippet:- https://jsfiddle.net/bmpp663q/ 工作片段:-https: //jsfiddle.net/bmpp663q/

To make this work you need to check if there are any checked checkboxes when any of their states change. 为了使这项工作有效,您需要检查任何状态更改时是否有任何选中的复选框。

To do this you can attach a change event handler to them all which then uses the :checked selector to determine if any have been chosen and then enable/disable the button as required. 为此,您可以将change事件处理程序附加到它们上,然后使用:checked选择器确定是否已选择任何一个,然后根据需要启用/禁用按钮。 Try this: 尝试这个:

 $('.form-check-input').change(function() { $('#next').prop('disabled', !$('.form-check-input:checked').length); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label> <input class="form-check-input tab1_chk" type="checkbox" name="2[]" value="1"> Option 1 </label> <label> <input class="form-check-input tab1_chk" type="checkbox" name="2[]" value="2"> Option 2 </label> <label> <input class="form-check-input tab1_chk" type="checkbox" name="2[]" value="2"> Option 3 </label><br /><br /> <button type="button" class="tab1_btn" name="next" id="next" disabled="true">Next</button> 

Try below code its working for me: 尝试下面的代码为我工作:

$(document).ready(function(){
    checkboxDisable()

    $(".tab1_chk").click(function(){
    checkboxDisable();
  });


  function checkboxDisable(){
    if($(".tab1_chk:checked").length > 0)
    {
      $('.tab1_btn').prop('disabled', false);

    }else{
      $('.tab1_btn').prop('disabled', true);

    }
  }
});

Event handler is a great solution... Loved it!! 事件处理程序是一个很好的解决方案……喜欢它! And if you want to do this on your page load you can do it by making a slight change to you existing code like: 如果要在页面加载时执行此操作,可以对现有代码进行一些更改,例如:

if ($(".tab1_chk").is(':checked')) {
  $('.tab1_btn').prop('disabled', false);
} else {
  $('.tab1_btn').prop('disabled', true);
}

It works, I tried it with your code!! 它有效,我用您的代码尝试过!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM