在母版中使用JavaScript
在母版中使用javascrip的朋友都会遇到document.getElementById("***")获取不到值的问题
问题描述见:http://www.cnblogs.com/lushuicongsheng/archive/2010/12/14/1905713.html
问题的根源在自己写的aspx文件中控件的ID和运行生成的HTML总的控件ID不一致,对比后发现多了一个ctl00$ContentPlaceHolder1$的前缀 。
解决问题的方法:在控件的中加入 ClientIDMode="Static",这样一来,运行前后的ID就会一致了。
代码示例:
<asp:DropDownList ID="ddl_system" ClientIDMode="Static" runat="server" >
<asp:ListItem Value="Name">系统名称</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="测试页面间的ddl传值" CssClass="btn_sub" OnClientClick="openDia();return false;" />
<script type="text/javascript">
function openDia() {
var ddl = document.getElementById("ddl_system");
var index = ddl.selectedIndex;
var itemValue = ddl.options[index].value;
var itemText = ddl.options[index].text;
alert(itemValue);
}
</script>
<asp:ListItem Value="Name">系统名称</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="测试页面间的ddl传值" CssClass="btn_sub" OnClientClick="openDia();return false;" />
<script type="text/javascript">
function openDia() {
var ddl = document.getElementById("ddl_system");
var index = ddl.selectedIndex;
var itemValue = ddl.options[index].value;
var itemText = ddl.options[index].text;
alert(itemValue);
}
</script>