【问题标题】:Disable checkbox on radio button click单击单选按钮时禁用复选框
【发布时间】:2019-05-14 06:58:41
【问题描述】:

我有 2 个单选按钮,如果单击单选按钮,我想禁用 array 中的复选框。这是.net 上一篇文章中的一些代码。

我使用ASPclassic,并希望在更改此代码或最能实现我的目标的代码方面获得帮助。

<input type="radio" name="group" value="value1">
<input type="radio" name="group" value="value2">

This is the checkbox that is in the loop for the array:

<input type="checkbox" name="items" value="<%=Rs("item") %>">


 $(document).ready(function () {

     $('#<%= rbRadioButton.ClientID %> input').change(function () {
         // The one that fires the event is always the
         // checked one; you don't need to test for this
         alert($(this).val());
     });
 });

【问题讨论】:

  • 请提供此代码附带的 HTML。
  • array.forEach(el =&gt; el.disabled = true)
  • @Tibrogargan html 已发布。

标签: javascript jquery checkbox


【解决方案1】:

使用.prop() 方法怎么样。

// Disable checkbox
$("input[value='value1']").change(function() {
  $("input[name='items']").prop('disabled', true);
});

// Enable checkbox
$("input[value='value2']").change(function() {
  $("input[name='items']").prop('disabled', false);
});

// Trigger reset button click
$("#btnReset").on("click", function() {
  $("input[name='group']").prop('checked', false);
  $("input[name='items']").prop('checked', false);
  $("input[name='items']").prop('disabled', false);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<fieldset>
  <legend>radio:</legend>
  <input type="radio" name="group" value="value1">disabled
  <input type="radio" name="group" value="value2">enabled
</fieldset>

<fieldset>
  <legend>checkbox:</legend>
  <input type="checkbox" name="items" value="1">
  <input type="checkbox" name="items" value="2">
  <input type="checkbox" name="items" value="3">
  <input type="checkbox" name="items" value="4">
  <input type="checkbox" name="items" value="5">
</fieldset>

<input type="button" value="Reset" id="btnReset">

【讨论】:

  • 做得很好,谢谢。有没有办法,如果用户改变主意清除(除了明显的刷新)来删除复选框禁用选择复选框?也许是一个不刷新页面的清除按钮。或者,如果用户要单击其中一个禁用的复选框,那么它会清除,做出选择然后禁用收音机?再次感谢!
  • @AspClassic-Guy 好的,感谢您的提示。请查看我的更新答案。
  • 现在这与之前的选择有何关联?有点困惑在哪里最好放置它。谢谢
  • 我明白了,谢谢。在我的情况下,最好有一个按钮来清除所有选择。最好使用什么?再次感谢您。
  • 发现了我的问题。我的第三台收音机里有一个大写字母。非常感谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多