首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何根据选择的单选按钮禁用按钮?

如何根据选择的单选按钮禁用按钮?
EN

Stack Overflow用户
提问于 2017-02-12 23:14:34
回答 2查看 845关注 0票数 0

我有3个div组,每个组包含一个单选按钮、一个标签和一个按钮。我希望能够启用与所选单选按钮相同的组内的按钮,并禁用来自其他组的按钮。

代码语言:javascript
运行
复制
<div class="row">
  <div class="col-md-6">
    <!-- when this radio button is checked... -->
    <input id="input-1" type="radio" name="disable-button" checked="checked">
    <label for="input-1">Input 1</label>
    <!-- this button is enabled -->
    <button type="button">ADD</button>
    <!-- ...and vice versa -->
  </div>
  <div class="col-md-6">
    <!-- when this radio button is not checked... -->
    <input id="input-2" type="radio" name="disable-button">        
    <label for="input-2">Input 1</label>
    <!-- ...this button should be disabled -->
    <button type="button" disabled>ADD</button>
    <!-- ...and vice versa -->
  </div>
  <div class="col-md-6">
    <!-- when this radio button is not checked... -->
    <input id="input-3" type="radio" name="disable-button">
    <!-- ...this button should be disabled -->
    <label for="input-3">Input 1</label>
    <button type="button" disabled>ADD</button>
    <!-- ...and vice versa -->
  </div>
</div>

有没有办法通过jQuery来做到这一点?

编辑:如果可能的话,我更希望解决方案是动态的,以防我需要添加更多的组。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-12 23:23:54

我将使用以下jQuery代码:

代码语言:javascript
运行
复制
$('input[type=radio]').change(function(){
  $('button').prop('disabled', true);
  $(this).parent().find('button').prop('disabled', false);
});

https://jsfiddle.net/dotspencer/6reL64ss/

票数 1
EN

Stack Overflow用户

发布于 2017-02-12 23:22:24

在父div块之后添加以下代码。

代码语言:javascript
运行
复制
 $(input[type=radio]).change(function(){
     $(this).is(':checked') && $(this).parent().find('button').prop('disabled', false) || $(this).parent().find('button').prop('disabled', true);
  });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42194403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档