简体   繁体   English

如何在选中复选框之前禁用按钮?

[英]How can I disable a button until a checkbox is checked?

I would like to have a form button disabled, until a user clicks a checkbox.我想禁用表单按钮,直到用户单击复选框。

This is the code i have (last part of the form)这是我的代码(表格的最后一部分)

<div class="checkbox">
    <input id="check" name="checkbox" type="checkbox">
    <label for="checkbox">
      Some Text Here
    </label>
  </div>
  <input type="submit" name="anmelden" class="button" id="btncheck" value="Send" />

I tried the following我尝试了以下

$('#check').click(function(){

if($(this).attr('checked') == false){
     $('#btncheck').attr("disabled","disabled");   
}
else
    $('#btncheck').removeAttr('disabled');
});

But this is not working and I am not sure why.但这不起作用,我不知道为什么。 I don't know how I can disable the button (should be disabled also visually).我不知道如何禁用按钮(也应该在视觉上禁用)。

Solution (thanks to Rory McCrossan ): http://jsfiddle.net/0hvtgveh/解决方案(感谢Rory McCrossan ): http : //jsfiddle.net/0hvtgveh/

Your logic is a little off, try this:你的逻辑有点不对,试试这个:

$('#check').change(function () {
    $('#btncheck').prop("disabled", !this.checked);
}).change()

Updated fiddle更新的小提琴

Note that the UI of the button does not update when disabled, however the disabled property does change.请注意,按钮的 UI 在禁用时不会更新,但是disabled属性会发生变化。 You would probably want to add some CSS styling to make it obvious that the button is disabled.您可能想要添加一些 CSS 样式以明确显示该按钮已禁用。

Also, I changed the code to use the change event instead of click to better cater for people who navigate using the keyboard.此外,我更改了代码以使用change事件而不是click以更好地满足使用键盘导航的人的需求。

Try with on change handler:尝试更改处理程序:

 $(function() { var chk = $('#check'); var btn = $('#btncheck'); chk.on('change', function() { btn.prop("disabled", !this.checked);//true: disabled, false: enabled }).trigger('change'); //page load trigger event });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="checkbox"> <input id="check" name="checkbox" type="checkbox"> <label for="check">Some Text Here</label><!-- for must be the id of input --> </div> <input type="submit" name="anmelden" class="button" id="btncheck" value="Send" />

You need to update property, so use .prop() instead of .attr()你需要更新属性,所以使用.prop()而不是.attr()

$('#check').click(function() {
    $('#btncheck').prop("disabled", this.checked == false);
});

A Good read .prop() vs .attr()一个很好的阅读.prop() 与 .attr()

$('#btncheck').attr("disabled",true);  //Initially disabled button when document loaded.
$('#check').click(function(){
    $('#btncheck').attr("disabled",!$(this).is(":checked"));   
})

When you click on checkbox .is(":checked") return true if it is checked other wise return false.当您单击复选框时,如果选中.is(":checked")返回 true,否则返回 false。 According to selection of your checkbox button it will enable/disable button.根据您选择的复选框按钮,它将启用/禁用按钮。

Demo演示

You have incorrect condition in if statement.use: if statement.use 中的条件不正确:

$('#check').click(function(){
 if(!$(this).is(':checked')){
    $('#btncheck').attr("disabled","disabled");   
 }else
    $('#btncheck').removeAttr('disabled');
 });

Demo演示

use if($(this ).prop( "checked" )) , instead of if($(this).attr('checked') == false)使用if($(this ).prop( "checked" )) ,而不是if($(this).attr('checked') == false)

http://jsfiddle.net/0hvtgveh/4/ http://jsfiddle.net/0hvtgveh/4/

$('#check').click(function(){

if($(this ).prop( "checked" )){
     $('#btncheck').attr("disabled","disabled");   
}
else
    $('#btncheck').removeAttr('disabled');
});

That is going to need a bit of logic :这将需要一些逻辑:

   $(function(){
      const button = $('#submit'); // The submit input id 
      button.attr('disabled', 'disabled');
      $('#checkbox').change(function() { // The checkbox id 
          if (this.checked){
            button.removeAttr('disabled')
            .css("cursor", "pointer");
          } else {
            button.attr('disabled', 'disabled')
            .css( "cursor", "not-allowed" );
          }
      });
  });

Remember to set your css Submit Button attribute to cursor: not-allowed;请记住将您的 css Submit Button 属性设置为cursor: not-allowed; hence can disable it upon page load.因此可以在页面加载时禁用它。

PS : This code goes to the bottom of the page. PS:此代码转到页面底部。 If it turns out that you have a separate file for it, do not forget to wrap it around:如果事实证明您有一个单独的文件,请不要忘记将其包装起来:

$(document).ready(function() { // code goes here });

I believe another way to handle this problem is by using document.getElementById:我相信处理这个问题的另一种方法是使用 document.getElementById:

if($(this).prop("checked")) {
    document.getElementById('#btncheck').disabled = false;
} else {
    document.getElementById('#btncheck').disabled = true;
}

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

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