【问题标题】:Disable a button in a radio button group禁用单选按钮组中的按钮
【发布时间】:2016-09-01 19:46:52
【问题描述】:

我有一个单选按钮组,我想动态启用/禁用按钮。

这是单选按钮组代码:

$("#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>

我已经尝试了以下所有这些选项:

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

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

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

但它们都不起作用。最后一个按钮看起来被禁用,但您仍然可以单击它们并触发事件。

任何想法表示赞赏:)

【问题讨论】:

  • $("#option1").attr('disabled',true) $("#option1").attr('disabled','disabled');都为我工作。您使用的是什么浏览器/jQuery 版本?

标签: javascript jquery html css twitter-bootstrap


【解决方案1】:

要禁用组中的引导单选按钮,您需要禁用相应的标签,并且需要处理禁用标签的点击事件。

$(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>

【讨论】:

  • 感谢您的回答!我认为问题出在代码的attr("disabled", 'disabled') 部分,因为我尝试将"disabled" 属性手动插入到html 代码&lt;input&gt;&lt;label&gt; 中,但该按钮仍然可点击并触发事件。有没有其他方法可以禁用输入,而不使用"disabled" 属性。
  • 但是你看如果我在你的代码 sn-p 中添加$("input[name='options']").closest('label').click(function() { alert("Clicked!"); });,即使按钮具有属性"disabled",事件也会被触发。 "disabled"属性不应该防止点击等事件被触发吗?
  • @patriciasmith 抱歉耽搁了。我在我的 sn-p 中添加了你的点击处理程序,我在 html 中添加了一个按钮,将活动状态从收音机 1 交换到收音机 2,因为我的 sn-p 禁用收音机 1。此外,我添加了一个侦听器:$(' .btn-group label').on('click', function(e) {。当你点击一个标签时,如果它被禁用,所有动作都将停止。我希望这会对你有所帮助。如果这是什么,请告诉我您正在寻找。谢谢。
  • 这正是我一直在寻找的,非常感谢!
【解决方案2】:

您的 jquery 选择器只关注第一个输入。使用一个选择器,其中包含您组的所有收音机,例如:

$(".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>

【讨论】:

  • 感谢您的回答。但问题不在于选择器,问题在于即使我使用prop("disabled", true) 禁用输入,该按钮仍然可以点击并触发点击事件。即使您在&lt;input type="radio" class="check" name="options" id="option1" checked&gt; 中手动插入 disabled 属性,该按钮仍然没有被禁用,我不明白为什么。
【解决方案3】:

你试过了吗?

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

【讨论】:

  • 是的,选择器部分工作正常 - 问题是向
  • 你需要禁用这个按钮的点击事件吗?
  • 如果你想禁用点击事件它完全不同......你必须使用bind / unbind或`on / off。那么您能否通过您的点击事件代码?你的点击事件中有什么?
  • 但是disable属性不应该自动阻止鼠标点击触发的所有事件吗?这是我的事件代码:$("#button-group :input").change(function() {...});
  • 点击事件不在您的输入上,而是在标签上。如果您在标签上设置 disabled 属性,这将以图形方式禁用,但事件始终存在。但是,如果您禁用真实输入,这将禁用事件并以图形方式禁用。试试我的小提琴来了解jsfiddle.net/bze5ujom/3