如何从单选按钮列表中更改已选择的单选按钮的颜色

时间:2016-03-31 08:33:47

标签: javascript jquery asp.net radiobuttonlist

我有一个单选按钮列表。最初,它被选中的值被捕获并存储到数据库中。当我再次访问该页面时,我之前选择的单选按钮将显示为已选中。现在我想用不同的颜色更改/突出显示所选按钮。

<asp:RadioButtonList RepeatColumns="5" class="style_radio"   ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal"
    RepeatLayout="Table"  Width="100%">
    <asp:ListItem Text=""  Value="1000"></asp:ListItem>
    <asp:ListItem Text=""  Value="2000"></asp:ListItem>
    <asp:ListItem Text=""  Value="3000"></asp:ListItem>
    <asp:ListItem Text="" Value="4000"></asp:ListItem>
    <asp:ListItem  Text="" Value="5000"></asp:ListItem>
</asp:RadioButtonList>

在页面加载时我写了如下,

SqlCommand cmd = new SqlCommand("Select selected_salary from staff_details where staff_id="ACD11" ", con6);
SqlDataReader dr2 = cmd.ExecuteReader();
if (dr2.HasRows == true)
{
    while (dr2.Read())
    {
         string salary = dr2[0].ToString();
        RadioButtonList rb = (RadioButtonList)Page.FindControl("RadioButtonList1");
        rb.SelectedValue = salary;

    }
}

2 个答案:

答案 0 :(得分:1)

如果我理解你想要什么,目的是改变选中的单选按钮的颜色/外观,是吗?在这种情况下,您可以只定义已选中单选按钮的css,如:

[type="radio"]:checked 
{
     width: 16px;
     height: 16px;    
     background-image:  ...
}

希望得到这个帮助。

答案 1 :(得分:0)

您必须找到从RadioButtonList中选择的RadioButton。然后向其添加样式属性。像:

foreach(ListItem li in RadioButtonList1.Items)
        {
          if (li.Selected)
            {
               li.Attributes.Add("style", "color: Green");
            }
        }
相关问题