DropDownList添加客户端下拉事件操作

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了DropDownList添加客户端下拉事件操作脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

如果要想给 DropDownList 服务器控件添加客户端下拉事件,我们可以强制给它添加 onchange 事件,尽管在控件中没有这个方法的提示。添加完这个事件还不能达到目的,还要设置 AutoPostBack 属性为 false,不让它回发后台事件。
以下就是为大家分享的代码:

<htML XMlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <tITle>DropDownList添加客户端下拉事件</title>
  <script tyPE="text/javascript">
    function getDropDownList() {
      VAR ddl1 = document.getElementById("<%=ddl1.ClientID%>");
      var text = ddl1.options[ddl1.options.selectedIndex].text; //获取text值
      var value = ddl1.value;                  //获取value值
      alert("Text:" + ddl1.options[ddl1.options.selectedIndex].text + ", Value:" + ddl1.value); 
    }
  </script>
</head>
<body>
<form id="form1" runat="server">
  <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="false" onchange="getDropDownList();">
    <asp:ListItem Text="T1" Value="V1" Selected="True"></asp:ListItem>
    <asp:ListItem Text="T2" Value="V2"></asp:ListItem>
    <asp:ListItem Text="T3" Value="V3"></asp:ListItem>
  </asp:DropDownList>
</form>
</body>
</html>

总结一下,也就是说,要想给DropDownList下拉框添加客户端下拉事件,必须做两步工作,一是添加强制onchange事件,二是把 AutoPostBack属性设为false,就是这么简单

脚本宝典总结

以上是脚本宝典为你收集整理的DropDownList添加客户端下拉事件操作全部内容,希望文章能够帮你解决DropDownList添加客户端下拉事件操作所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: