如何根据某些条件切换css属性

时间:2013-01-06 11:26:22

标签: javascript jquery asp.net css radiobuttonlist

我有以下radiobutton列表:

<asp:Panel ID="pnl_select_sign" runat="server" Visible="false" Style="text-align: right">
        <asp:RadioButtonList ID="rb_select_sign" runat="server" AutoPostBack="true" RepeatDirection="Horizontal"
            OnSelectedIndexChanged="rb_select_sign_SelectedIndexChanged" CausesValidation="false"
            AppendDataBoundItems="true">
            <asp:ListItem Selected="True" Value="0">Normal</asp:ListItem>
            <asp:ListItem Value="1">Electronic</asp:ListItem>
        </asp:RadioButtonList>
    </asp:Panel>

我想更改css属性overflow-y

来自overflow-y :auto

;到overflow-y :hidden;

对于id = wrap_form

的div

如果我选择ListItem with Value = 1;


来自 FireBug

<td>
<input id="ctl00_ContentPlaceHolder1_rb_select_sign_0" type="radio" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$rb_select_sign$0\',\'\')', 0)" value="0" name="ctl00$ContentPlaceHolder1$rb_select_sign">
<label for="ctl00_ContentPlaceHolder1_rb_select_sign_0">Normal</label>
</td>
<td>
<input id="ctl00_ContentPlaceHolder1_rb_select_sign_1" type="radio" checked="checked" value="1" name="ctl00$ContentPlaceHolder1$rb_select_sign">
<label for="ctl00_ContentPlaceHolder1_rb_select_sign_1">Electronic</label>
</td>

2 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

$('input[id^="ctl00_"]').click(function() {
 if ($(this).val() == 1) {
  $('#wrap_form').css('overflow-y', 'hidden');
 }
})

这是jsfiddle

答案 1 :(得分:1)

请注意,如果您使用建议的javascript答案,则css将在页面回发后恢复正常。您已经在RadioButtonList上使用了回发,因此我认为您只需更改服务器端的样式。

<强>更新

将此项放入您的方法处理列表中的项目选择更改:

protected void rb_select_sign_SelectedIndexChanged(object sender, EventArgs e)
{
    if (rb_select_sign.SelectedValue == "1")
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "styleChangeScript", "<script type='text/javascript'>var divToChange = document.getElementById('wrap_form'); if(divToChange) { divToChange.style.overflowY = 'hidden' }</script>", false);
    }
    //the rest of the original code of this method should be here
    //...
}

要在点击导致回发的其他按钮等时使其工作,您应该在您的页面的Page_Load事件中放置相同的代码行(或者只是将其放在一个单独的方法中并从两个地方调用它)