简体   繁体   English

Jquery如何禁用提交按钮,直到选中单选按钮和复选框

[英]Jquery how to disable submit button until radio button and checkbox has been checked

I have a submit button which is disabled by default.我有一个默认禁用的提交按钮。 Now I have two radio buttons and a checkbox and the submit button is only allowed to be enabled when one of the radio buttons has been checked AND the checkbox has been checked.现在我有两个单选按钮和一个复选框,并且只有在选中其中一个单选按钮并且选中了复选框时才允许启用提交按钮。

Right now, I can simply check the checkbox and the submit button is enabled.现在,我可以简单地选中复选框并启用提交按钮。

<div>
  <label for="check1">
     <input type="radio" name="radio" id="check1">Value 1
  </label>

  <label for="check2">
     <input type="radio" name="radio" id="check2">Value 2
  </label>
</div>

<div id="terms">
   <input type="checkbox" name="test" value="1">Check
 </div>

<div>
  <button type="submit" id="btn">Send</button>
</div>

and the JS:和JS:

$('button[type="submit"]').prop("disabled", true);

$('input[name="test"]').on("click", function() {
  $(' button[type="submit"]').prop("disabled", !$('input[name="test"]').prop("checked"));
});

I made an jsfiddle for it.我为它制作了一个jsfiddle

So, how can I achieve that?那么,我怎样才能做到这一点?

You can check the length of checked radio button:您可以检查选中单选按钮的长度:

 $('button[type="submit"]').prop("disabled", true); $('input[name="test"], input[name="radio"]').on("click", function() { if($('input[name="radio"]:checked').length){ $(' button[type="submit"]').prop("disabled", !$('input[name="test"]').prop("checked")); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <label for="check1"> <input type="radio" name="radio" id="check1">Value 1 </label> <label for="check2"> <input type="radio" name="radio" id="check2">Value 2 </label> </div> <div id="terms"> <input type="checkbox" name="test" value="1">Check </div> <div> <button type="submit" id="btn">Send</button> </div>

Create one common function to validate all checkboxes and radio button.创建一个通用函数来验证所有复选框和单选按钮。

Check below sample.检查以下示例。

 $('button[type="submit"]').prop("disabled", true); $('#check1, #check2, input[name="test"]').on("change", function() { validate(); }); function validate() { var valid = true; if(!$('[name="radio"]:checked').val()) { valid = false; } if(!$('[name="test"]').prop('checked')) { valid = false; } $('button[type="submit"]').prop("disabled", !valid) }
 #terms { margin-top: 15px; } #btn { margin-top: 15px; background: red; padding: 5px; border: 0; } #btn::disabled { background: lightgrey; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <label for="check1"> <input type="radio" name="radio" id="check1" value="1">Value 1 </label> <label for="check2"> <input type="radio" name="radio" id="check2" value="2">Value 2 </label> </div> <div id="terms"> <input type="checkbox" name="test" value="1">Check </div> <div> <button type="submit" id="btn">Send</button> </div>

Add an onclick on both of them, and and if statement inside :在它们两个上添加一个 onclick,并在里面添加 if 语句:

 $('button[type="submit"]').prop("disabled", true); $('input[name="test"], input[name=radio]').on("click", function() { if ($('input[name=radio]:checked').length > 0) { $('button[type="submit"]').prop( "disabled", !$('input[name="test"]').prop("checked") ); } });
 #terms { margin-top: 15px; } #btn { margin-top: 15px; background: red; padding: 5px; border: 0; } #btn::disabled { background: lightgrey; }
 <div> <label for="check1"> <input type="radio" name="radio" id="check1">Value 1 </label> <label for="check2"> <input type="radio" name="radio" id="check2">Value 2 </label> </div> <div id="terms"> <input type="checkbox" name="test" value="1">Check </div> <div> <button type="submit" id="btn">Send</button> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>

https://jsfiddle.net/wxq0Lr5n/ https://jsfiddle.net/wxq0Lr5n/

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

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