【发布时间】:2017-10-14 19:01:10
【问题描述】:
我有两种类型的用户(临时/永久)的注册页面,我只需要向每个用户显示表单的一部分。我试图通过使用 jquery 隐藏某些 div 元素来实现这一点。问题是我的 DIV 元素暂时隐藏并在一秒钟内重新出现。我尝试查看多种解决方案,但似乎都没有。这是我的代码。
<script>
$(document).ready(function () {
$("#<%=releasetype.ClientID%>").click(function () {
var reltype = $("input[name='<%=releasetype.UniqueID%>']:radio:checked").val();
if (reltype == "Temporary") {
document.getElementById('pm').style.display = 'none';
} else if (reltype == "Permanent") {
document.getElementById('pm').style.display = 'block';
}
});
});
</script>
.aspx
<asp:RadioButtonList ID="releasetype" cssclass="radio" runat="server" AutoPostBack="True" RepeatDirection="Vertical">
<asp:ListItem>Temporary</asp:ListItem>
<asp:ListItem>Permanent</asp:ListItem>
</asp:RadioButtonList>
<div id="pm" class="form-horizontal">
<asp:Label ID="Label8" runat="server" Text="PrimaryID" Font-Bold="True"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</div>
我尝试使用 event.preventDefault(); & 返回错误;在我的代码中。但是这两种方法都没有阻止页面刷新。
【问题讨论】:
-
请显示实际的 HTML 代码,而不是我们无法轻易复制的服务器端代码。 minimal reproducible example
-
您好,您正在使用 ASP .NET Web 表单将单选按钮列表的 AutoPostBack 属性设置为 false
-
试过 vignesh,没有修复它。 - 实际上尝试将其设置为 false。还是一样
-
更新:需要两次点击才能隐藏,第一次点击隐藏 div 元素一秒钟,第二次点击后隐藏元素
-
如果你只做前端工作,那么你就不必使用
Server Controls。为什么需要在单选按钮上Postback?你真的需要/它做任何工作吗?如果没有,只需使用纯 HTMLinput