Javascript 如何根据条件切换css属性
我有以下单选按钮列表:Javascript 如何根据条件切换css属性,javascript,jquery,asp.net,css,radiobuttonlist,Javascript,Jquery,Asp.net,Css,Radiobuttonlist,我有以下单选按钮列表: <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" OnSelectedInde
<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>
像这样的方法应该会奏效:
$('input[id^="ctl00_"]').click(function() {
if ($(this).val() == 1) {
$('#wrap_form').css('overflow-y', 'hidden');
}
})
请注意,如果使用建议的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
//...
}
protectedvoid rb\u select\u sign\u SelectedIndexChanged(对象发送方,事件参数e)
{
如果(rb_select_sign.SelectedValue==“1”)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),“styleChangeScript”,“var divToChange=document.getElementById('wrap_form');if(divToChange){divToChange.style.overflowY='hidden'},false);
}
//此方法的其余原始代码应在此处
//...
}
要使其在单击其他按钮等导致回发时正常工作,您应该在页面的
页面加载事件中放置相同的代码行(或者将其放在单独的方法中并从两个位置调用)。我做了一个小改动,再次检查确保您在页面(或母版页)中引用了jQuery库。我引用了该文件,我认为问题在于控件进行了回发。请告诉我divwrap_form
是由您放在页面的aspx中还是自动生成的(您在aspx中看不到)。它在母版页中:
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
//...
}