JS在更新面板后重置
JS gets reset after update panel
我有一个JS函数,它显示和隐藏下拉列表,并在下拉列表更改时调用。然而,我也在使用其他东西的更新面板,每次我选择某个东西时,JS都会重置,我想继续显示的额外下拉列表也会重置,即再次隐藏(尽管它不会重置值,这很好)。
默认情况下,额外的下拉列表是隐藏的。例如,当用户选择2时,它将显示2个下拉列表。如何在更新面板刷新后使下拉列表不再隐藏。
这是我的代码:
<!-- Preference CSS -->
<link rel="Stylesheet" type="text/css" href="Styles/Preference.css" />
<script type="text/javascript" language="javascript">
$(document).ready(function () {
//Loading Page
});
function changeNumOfRooms(ddl) {
if (ddl.value == "1") {
document.getElementById("<%=roomNumDDL1.ClientID %>").style.display = "";//changes the number of room number fields
document.getElementById("<%=roomNumDDL2.ClientID %>").style.display = "none";
document.getElementById("<%=roomNumDDL3.ClientID %>").style.display = "none";
}
if (ddl.value == "2") {
document.getElementById("<%=roomNumDDL1.ClientID %>").style.display = "";//changes the number of room number fields
document.getElementById("<%=roomNumDDL2.ClientID %>").style.display = "";
document.getElementById("<%=roomNumDDL3.ClientID %>").style.display = "none";
}
if (ddl.value == "3") {
document.getElementById("<%=roomNumDDL1.ClientID %>").style.display = "";//changes the number of room number fields
document.getElementById("<%=roomNumDDL2.ClientID %>").style.display = "";
document.getElementById("<%=roomNumDDL3.ClientID %>").style.display = "";
}
}
</script>
<%-- Body Content --%>
<asp:UpdatePanel ID="UpdateModule" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="modCodeDDL" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="modTitleDDL" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<a href="#" title="Select the module code (Mandatory Field)">Module Code*</a>
<asp:DropDownList ID="modCodeDDL" runat="server" OnSelectedIndexChanged="modCodeToTitle" AutoPostBack="True" />
<a href="#" title="The name of the module e.g. 'Team Projects'. Mandatory Field.">Module Title*</a>
<asp:DropDownList ID="modTitleDDL" runat="server" OnSelectedIndexChanged="modTitleToCode" AutoPostBack="True" Style="width: 250px;" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdateBuilding" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="buildingDDL" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="parkDDL" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<a href="#" title="Choose the park that you would like. Mandatory Field.">Park*</a><!-- PARK -->
<asp:DropDownList class="form-control" ID="parkDDL" Style="width: 150px;" OnSelectedIndexChanged="parkToBuilding" AutoPostBack="true" runat="server">
<asp:ListItem Text="ALL" Value="ALL"></asp:ListItem>
<asp:ListItem Text="Central" Value="C"></asp:ListItem>
<asp:ListItem Text="West" Value="W"></asp:ListItem>
<asp:ListItem Text="East" Value="E"></asp:ListItem>
</asp:DropDownList>
<a href="#" title="Choose the building that you would like">Building</a><!-- BUILDING -->
<asp:DropDownList class="form-control" AutoPostBack="true" ID="buildingDDL" runat="server" style="width:300px;" OnSelectedIndexChanged="buildingToRoom"></asp:DropDownList>
<a href="#" title="Do you require a particular room? If so select here">Room Number</a><!-- ROOM NUMBER -->
<asp:DropDownList class="form-control" ID="roomNumDDL1" title="For room 1" runat="server"></asp:DropDownList>
<asp:DropDownList class="form-control" ID="roomNumDDL2" title="For room 2" runat="server" style="display:none;"></asp:DropDownList>
<asp:DropDownList class="form-control" ID="roomNumDDL3" title="For room 3" runat="server" style="display:none;"></asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
<a href="#" title="Enter the number of rooms required for this lecture e.g. 1,2,...">Number of rooms*</a><!-- NUMBER OF ROOMS -->
<asp:DropDownList ID="numOfRoomsDDL" runat="server" onchange="changeNumOfRooms(this)">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
所有JS内容都是在ASP.NET之上调用的。
提前感谢!
必须在$(document).ready
和_endRequest事件中调用DropDownList可见性更改函数。检查我下面的代码:
$(document).ready(function () {
var numOfRoomsDDL = document.getElementById("<%= numOfRoomsDDL.ClientID%>");
changeNumOfRooms(numOfRoomsDDL);
});
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(function () {
var numOfRoomsDDL = document.getElementById("<%= numOfRoomsDDL.ClientID%>");
changeNumOfRooms(numOfRoomsDDL);
});
相关文章:
- 有条件更新d3.js力图中节点的最佳方法
- d3.js用按钮更新条形图工具提示
- React js更改状态不会更新组件
- 谷歌地图/Angular JS:地图更新并不总是反映数据,并且可以't删除标记
- 如何让React JS点击处理程序在执行时更新DOM
- 更新从Mysql查询检索到的数据?Node.js节点mysql
- JS在更新面板后重置
- Node.js错误“;ReferenceError:全局未定义“;在从0.10.2更新到0.12.2之后
- 使用PHP更新external.js中的Javascript变量
- Angular JS-文本框未在独立范围内更新
- 在第三次onPageClick事件后,使用twbspagination插件和AJAX更新JS分页
- 基于元素ID更新JS对象
- 持续更新 JS 变量的 HTML 显示
- 更新JS类的属性基于其他属性
- 在angular $scope中更新JS数组
- Jquery .html()似乎没有更新JS代码
- React Native,自动更新.js文件
- 如何通过ajax更新js对象类型变量
- 如何让grunt用户更新JS来引用我们的图像
- 基于本地存储更新JS对象