在 ASP.NET RadioButtonList ListItem 上设置 CSS 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1070960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set a CSS class on an ASP.NET RadioButtonList ListItem
提问by y0mbo
Is there any way to set a CSS class on an input item in a radio button list? I'd like to reference these form values by class in jQuery.
有没有办法在单选按钮列表中的输入项上设置 CSS 类?我想在 jQuery 中按类引用这些表单值。
Given an ASP.NET RadioButtonList with classes set on the ListItems:
给定一个在 ListItems 上设置了类的 ASP.NET RadioButtonList:
<asp:RadioButtonList ID="RadioButtonList" runat="server">
<asp:ListItem class="myClass" Text="Yes" Value="1" />
<asp:ListItem class="myClass" Text="No" Value="0" />
</asp:RadioButtonList>
Will render as:
将呈现为:
<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>
Unfortunately, the "myClass" class is added to the <span>
that contains the radio button item, not the <input>
itself. How could I get it to look like this instead:
不幸的是,“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>
(This does not even get into the issue of adding the classes to dynamically bound RadioButtonLists.)
(这甚至没有涉及将类添加到动态绑定 RadioButtonLists 的问题。)
采纳答案by SolutionYogi
Yes, RadioButtonList renders horrible HTML.
是的,RadioButtonList 呈现可怕的 HTML。
Anyway, it's still easy to get them from jQuery.
无论如何,从 jQuery 获取它们仍然很容易。
Try
尝试
$('span.myclass input:radio')
Above will get all the radio buttons inside the span with class 'myclass'.
上面将使用类“myclass”获取跨度内的所有单选按钮。
回答by JamesEggers
An alternative you may want to try to do is change your CSS class to accomindate the Server Control Rendering.
您可能想要尝试做的另一种选择是更改 CSS 类以适应服务器控件呈现。
So in your CSS instead of the following:
所以在你的 CSS 中而不是以下内容:
.myClass { ... }
You Use this:
你使用这个:
.myClass input { ... }
I don't know if you can set the class attribute (via class or CSSClass) in a declarative manner; however, the above should give you a work around.
不知道能不能用声明的方式设置class属性(通过class或者CSSClass);然而,以上应该给你一个解决方法。
回答by Punit Vora
untested
未经测试
$('.myclass input:radio').each(function() {
this.css({'style-name':'style-value'})
)};
once the list is rendered, you can possibly manipulate the css client side using the above statement.
呈现列表后,您可以使用上述语句操作 css 客户端。
回答by Relster
Using .NET you could create an event handler to the RowBinding event. This would allow you to access the individual controls within each item in the list. Once you pull the RadioButton control, you could programaticaly set it's CssClass to apply your class on the RadioButton level.
使用 .NET,您可以为 RowBinding 事件创建一个事件处理程序。这将允许您访问列表中每个项目中的各个控件。拉出 RadioButton 控件后,您可以以编程方式设置它的 CssClass 以在 RadioButton 级别应用您的类。
You could also use jquery to find the radio button controls that have myClass applied, and then apply another class to that.
您还可以使用 jquery 查找应用了 myClass 的单选按钮控件,然后对其应用另一个类。
Third, you could just change the definition of MyClass to also specify that it's only applied to input elements under elements that have MyClass applied.
第三,您可以更改 MyClass 的定义,以指定它仅应用于应用了 MyClass 的元素下的输入元素。
回答by Markive
the desigeek response is pretty good..
desigeek的反应非常好..
To get this to work for radiobuttons I had to do this on document.ready()
为了使它适用于单选按钮,我必须在 document.ready() 上执行此操作
$('#<%=radRisksMarginTrading.ClientID %>_0').addClass("validate[required]");
$('#<%=radRisksMarginTrading.ClientID %>_1').addClass("validate[required]");
I'm sure there is a much better way to do it by looping but I need a quick fix for 5 radiobutton lists..
我确信有一个更好的方法可以通过循环来做到这一点,但我需要快速修复 5 个单选按钮列表..