在ASP.NET RadioButtonList ListItem上设置CSS类

y0m*_*mbo 24 css asp.net radiobuttonlist listitem

有没有办法在单选按钮列表中的输入项上设置CSS类?我想在jQuery中按类引用这些表单值.

给定一个ASP.NET RadioButtonList,在ListItems上设置类:

 <asp:RadioButtonList ID="RadioButtonList" runat="server">
      <asp:ListItem class="myClass" Text="Yes" Value="1" />
      <asp:ListItem class="myClass" Text="No" Value="0" />
 </asp:RadioButtonList>
Run Code Online (Sandbox Code Playgroud)

将呈现为:

 <span id="RadioButtonList" class="radioButtonListField myClass">
     <span class="myClass">
          <input id="RadioButtonList_0" type="radio" value="1" 
               name="RadioButtonList"/>
          <label for="RadioButtonList_0">Yes</label>
     </span>
     <span class="myClass">
          <input id="RadioButtonList_1" type="radio" value="0" 
               name="RadioButtonList"/>
          <label for="RadioButtonList_1">No</label>
     </span>
 </span>
Run Code Online (Sandbox Code Playgroud)

不幸的是,"myClass"类被添加到<span>包含单选按钮项而不是它<input>自身.我怎么能让它看起来像这样:

 <span id="RadioButtonList" class="radioButtonListField myClass">
     <span>
          <input id="RadioButtonList_0" class="myClass" type="radio" value="1" 
               name="RadioButtonList"/>
          <label for="RadioButtonList_0">Yes</label>
     </span>
     <span>
          <input id="RadioButtonList_1" class="myClass" type="radio" value="0" 
               name="RadioButtonList"/>
          <label for="RadioButtonList_1">No</label>
     </span>
 </span>
Run Code Online (Sandbox Code Playgroud)

(这甚至没有涉及将类添加到动态绑定RadioButtonLists的问题.)

Sol*_*ogi 10

是的,RadioButtonList呈现可怕的HTML.

无论如何,从jQuery获取它们仍然很容易.

尝试

$('span.myclass input:radio')
Run Code Online (Sandbox Code Playgroud)

上面将使用类'myclass'获取跨度内的所有单选按钮.


Jam*_*ers 9

您可能想要尝试的另一种方法是更改​​CSS类以适应服务器控件渲染.

所以在你的CSS而不是以下:

.myClass { ... }
Run Code Online (Sandbox Code Playgroud)

你用这个:

.myClass input { ... }
Run Code Online (Sandbox Code Playgroud)

我不知道你是否可以以声明的方式设置class属性(通过类或CSSClass); 但是,上面应该给你一个解决方法.