javascript - 设置为 buttonset() 后禁用单选按钮

标签 javascript jquery jquery-ui

我有 3 个单选按钮

<div id="test">
<input type="radio" id="time1" name="radio" value="1" /><label for="time1">Test 1</label>
<input type="radio" id="time2" name="radio" value="2" /><label for="time2">Test 2</label>
<input type="radio" id="time3" name="radio" value="3" /><label for="time3">Test 3</label>
</div>

在 jquery 中

$("#test").buttonset();

在那之后,我想禁用它们(当然,禁用放在 if 语句中)

$("#test input").attr("disabled", true);  //or
$("#test input").prop("disabled", true);

但它不起作用,按钮仍然可用。

最佳答案

您使用的是 jQuery-UI,将单选按钮更改为 buttonset 后,它们不再影响 UserInterface,因为它包含:

<div id="test" class="ui-buttonset">
<input type="radio" id="time1" name="radio" value="1" class="ui-helper-hidden-accessible"><label for="time1" aria-pressed="false" class="ui-button ui-widget ui-state-default ui-button-text-only ui-corner-left" role="button" aria-disabled="false"><span class="ui-button-text">Test 1</span></label>
<input type="radio" id="time2" name="radio" value="2" class="ui-helper-hidden-accessible"><label for="time2" aria-pressed="false" class="ui-button ui-widget ui-state-default ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text">Test 2</span></label>
<input type="radio" id="time3" name="radio" value="3" class="ui-helper-hidden-accessible"><label for="time3" aria-pressed="true" class="ui-button ui-widget ui-state-default ui-button-text-only ui-corner-right ui-state-active" role="button" aria-disabled="false"><span class="ui-button-text">Test 3</span></label>
</div>

所以你需要 jQuery UI 函数来禁用“按钮”(它们是 spans!)

$("#test input").button("disable")

Live DEMO

jQuery-UI source

关于javascript - 设置为 buttonset() 后禁用单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10513729/

相关文章:

javascript - 普通JS : Select all elements with class within parent based on cursor position

javascript - 在 pdf.js 中复制 Canvas

javascript - 标签文本框的制作方法

javascript - 通过自定义数据属性值查找元素

javascript - 使用动态内容时 jQuery UI 工具提示不会关闭

javascript - Parse - JavaScript 指针查询,从指针返回解析对象的问题

javascript - 将 JavaScript 数组的字符串表示形式转换为对象

javascript - 使用 jQuery 获取 child id

javascript - Jquery 包含特殊字符和小写字母

jquery - 使用 jQuery .remove() 仅删除第一个列表项