简体   繁体   English

如果选中一台收音机,则禁用按钮

[英]Disable button if one radio is checked

I have three radio each one has name started with delivery_option and a submit button with css class continue . 我有三个收音机,每个收音机的名称都以delivery_option开头,而带有CSS类的Submit按钮则为continue I want to test if a radio is checked the button must be enabled, otherwise it should be disabled. 我想测试是否选中了收音机,必须启用该按钮,否则应将其禁用。

i made the code below 我做了下面的代码

$(document).ready(function() {
  $('#checkout-delivery-step input:radio').each(function() {
    if ($("input:radio[name*='delivery_option']:checked").length != 0) {
      $('.continue').prop('disabled', false);
    } else {
      $('.continue').prop('disabled', true);
    }
  });
});

but it does not work, what was the issue? 但这不起作用,这是什么问题?

You are running the code only once . 您只运行一次代码。 The code has to be run every time when the radio button is clicked or changed . 每次单击或更改radio按钮时,都必须运行该代码。 So you need to use the following: 因此,您需要使用以下内容:

// Make this a function.
function checkProgress() {
  if ($("input:radio[name*='delivery_option']:checked").length != 0) {
    $('.continue').prop('disabled', false);
  } else {
    $('.continue').prop('disabled', true);
  }
}

$(function () {
  // Set the status once the doc loads.
  checkProgress();
  // Set it again when any of the radio buttons are clicked.
  $("input:radio[name*='delivery_option']").on("click change", checkProgress);
});

Snippet 片段

 // Make this a function. function checkProgress() { if ($("input:radio[name*='delivery_option']:checked").length != 0) { $('.continue').prop('disabled', false); } else { $('.continue').prop('disabled', true); } } $(function () { // Set the status once the doc loads. checkProgress(); // Set it again when any of the radio buttons are clicked. $("input:radio[name*='delivery_option']").on("click change", checkProgress); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h3>Group 1</h3> Option 1: <input type="radio" name="delivery_option_1" /> Option 2: <input type="radio" name="delivery_option_1" /> Option 3: <input type="radio" name="delivery_option_1" /> <h3>Group 2</h3> Option 1: <input type="radio" name="delivery_option_2" /> Option 2: <input type="radio" name="delivery_option_2" /> Option 3: <input type="radio" name="delivery_option_2" /> <p>Submit?</p> <input type="button" value="Submit" class="continue" disabled /> 

You can try something like this with removeAttr which will remove the attribute from any element which is already set. 您可以使用removeAttr尝试类似的操作,该操作将从已设置的任何元素中删除属性。

Also, for the radio you can do this way, once it is clicked then you can enable the button because it doesn't provide a way to deselect it. 此外,对于收音机,您可以执行此操作,一旦单击它,便可以启用该按钮,因为它没有提供取消选择它的方法。

Finally, the name for elements can be the same if it is a group and only the id must be unique. 最后,如果元素是一个组,则元素的名称可以相同,并且只有id必须是唯一的。 Check here . 在这里检查。

So, proper code will be 因此,正确的代码将是

<label for="delivery_option1">Option1:</label><input type="radio" id="delivery_option1" name="delivery_option" />
<label for="delivery_option2">Option2:</label> <input type="radio" id="delivery_option2" name="delivery_option" />
<label for="delivery_option3">Option3:</label><input type="radio" id="delivery_option3" name="delivery_option" />

 $(function(){ $("input:radio[name*='delivery_option']").click(function(){ $(".continue").removeAttr("disabled"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Option1: <input type="radio" name="delivery_option1" /> Option2: <input type="radio" name="delivery_option2" /> Option3: <input type="radio" name="delivery_option3" /> <input type="button" value="Submit" class="continue" disabled /> 

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

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