jquery 复选框启用/禁用单选按钮列表

标签 jquery

我在同一表行和不同的 td 中有一个复选框和禁用的单选按钮列表,并且只是在选中/取消选中复选框时尝试启用/禁用它。

<tr class = "main">
    <td colspan="2">
      <input id="CheckOne" type="checkOne" name="checkOne" />
      <label for="CheckOne">Some Text</label>
    </td>
    <td align="center">
        <table id="RadioButtonListOne" disabled="disabled" title="Choices" border="0">
        <tr>
         <td>
                    <span disabled="disabled">
                    <input id="RadioButtonListOne_0" type="radio" name="RadioButtonListOne" value="Y" disabled="disabled" />
                    <label for="RadioButtonListOne_0">Yes</label></span>
               </td>
               <td>
                    <span disabled="disabled"><input id="RadioButtonListOne_1" type="radio" name="RadioButtonListOne" value="N" disabled="disabled" />
                    <label for="RadioButtonListOne_1">No</label>
                    </span>
               </td>       
           </tr>
        </table>
     </td>
  </tr>

这就是原始服务器端 html 的样子:

   <tr class = "main">
       <td colspan="2">
            <asp:CheckBox ID="CheckBoxOne" runat="server" Text="Some Text"/>
       </td>
       <td align="center">
            <asp:RadioButtonList ID="RadioButtonListOne" RepeatDirection="Horizontal">
                  <asp:ListItem Value="Y">Yes asp:ListItem>
                  <asp:ListItem Value="N">No</asp:ListItem>
             </asp:RadioButtonList>
        </td>
   </tr>

由于某种原因,当我尝试做一些明显的事情时,即

    $('#<%= CheckBoxOne.ClientID %>').click(function()
    {
        if ($(this).is(":checked"))
        {
             $('#<%= RadioButtonListOne.ClientID%> input:radio').removeAttr('disabled');

        }
        else 
        {
              $('#<%= RadioButtonListOne.ClientID%> input:radio').attr('disabled', 'disabled');
        }
    });

然后不起作用,但从逻辑上讲它应该起作用。我在这里做错了什么?

最佳答案

为什么在 jquery 代码中包含 <%= RadioButtonListOne.ClientID% input:radio ,而且还缺少尾随 > 符号

$('#<%= RadioButtonListOne.ClientID%> input:radio').removeAttr('disabled');

你的jquery代码是否在同一页面中,以便在客户端下载之前由服务器处理?像 <%= RadioButtonListOne.ClientID%> 这样的东西在 php、asp 或 html.erb 等文件之外无法工作

您可以尝试将 if 检查更改为此

if ($(this).attr('checked') == 'checked') 

而不是.is(':checked')

关于jquery 复选框启用/禁用单选按钮列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2329646/

相关文章:

javascript - 父宽度和高度区域中的轻量级可拖动锁

javascript - 尝试使用 CSS/jQuery 在悬停时平滑/连续地制作正弦波链接下划线动画

javascript - 下拉菜单使用 data- 显示子菜单

javascript - 如果提交了新的 AJAX 请求,则取消 AJAX 请求

jquery.validate、jquery.metadata 和 html5 数据

javascript - Jquery 单击返回一个值并在另一个函数中使用它

javascript - 表单字段未存储在 mysql 数据库中

javascript - 使用 EaselJS 获取触摸设备上的 "mouse"位置

javascript - jQuery:如何使 css ('line-height' ) 跨浏览器工作

javascript - 当不是 'in view' 时如何卸载图像以节省内存?