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

编程学习 2021-07-04 22:41www.dzhlxh.cn编程入门
我们知道,DropDownList下拉框是一个服务器控件,有时候,有些朋友为了方便绑定DropDownList下拉框的选项,但又想在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,就是这么简单!

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板