简体   繁体   English

禁用单选按钮组中的按钮

[英]Disable a button in a radio button group

I have a radio button group and I would like to dynamically enable/disable buttons. 我有一个单选按钮组,我想动态启用/禁用按钮。

This is the radio button group code: 这是单选按钮组代码:

 $("#option1").prop("disabled", true); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div style="clear: both; display: inline;"> <div class="btn-group" data-toggle="buttons"> <label id="option1label" class="btn btn-info active"> <input type="radio" class="check" name="options" id="option1" checked>1 </label> <label id="option2label" class="btn btn-info"> <input type="radio" class="check" name="options" id="option2">2 </label> <label id="option3label" class="btn btn-info"> <input type="radio" class="check" name="options" id="option3">3 </label> </div> </div> 

I have tried all these options below: 我在下面尝试了以下所有选项:

$("#option1").prop("disabled", true);

$("#option1").attr("disabled", "disabled");

$("#option1label").attr("disabled", "disabled");

but none of them work. 但它们都不起作用。 With the last one the buttons look disabled, but you can still click on them and trigger events. 使用最后一个按钮看起来已禁用,但您仍然可以单击它们并触发事件。

Any ideas appreciated :) 任何想法赞赏:)

You jquery selector focuses only first input. 你的jquery选择器只关注第一个输入。 Use a selector wich include all the radio of your group, for example: 使用包含组中所有无线电的选择器,例如:

 $(".btn-group input[type='radio']").prop("disabled", true); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div style="clear: both; display: inline;"> <div class="btn-group" data-toggle="buttons"> <label id="option1label" class="btn btn-info active"> <input type="radio" class="check" name="options" id="option1" checked>1 </label> <label id="option2label" class="btn btn-info"> <input type="radio" class="check" name="options" id="option2">2 </label> <label id="option3label" class="btn btn-info"> <input type="radio" class="check" name="options" id="option3">3 </label> </div> </div> 

In order to disable a bootstrap radio button in a group you need to disable the corresponding label and you need to handle click events for the disabled label. 要禁用组中的引导单选按钮,您需要禁用相应的标签,并且需要处理禁用标签的单击事件。

 $(function () { // for only one radio button $("#option1").closest('label').attr("disabled", 'disabled'); // for all radio //$(":radio").closest('label').attr("disabled", 'disabled'); // // On click event check if current label is disabled. // If disabled, stop all. // $('.btn-group label').on('click', function(e) { if ($(this).is('[disabled]')) { e.preventDefault(); e.stopImmediatePropagation(); } }); $('#btnInfo').on('click', function (e) { var txt = $(':radio:checked').length == 1 ? 'Checked radio is: ' + $(':radio:checked').attr('id') : 'No radio checked' console.log(txt); }); $("input[name='options']").closest('label').click(function (e) { console.log('Lbale ' + this.id + ' Clicked!'); }); }); 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div style="clear: both; display: inline;"> <div class="btn-group" data-toggle="buttons"> <label id="option1label" class="btn btn-info active"> <input type="radio" class="check" name="options" id="option1">1 </label> <label id="option2label" class="btn btn-info"> <input type="radio" class="check" name="options" id="option2" checked>2 </label> <label id="option3label" class="btn btn-info"> <input type="radio" class="check" name="options" id="option3">3 </label> </div> </div> <button id="btnInfo" type="button" class="btn btn-primary">Show currently checked radio button</button> 

你试过这个吗?

$('input[name= options]').attr("disabled",true);

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

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