如果要想給 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,就是這么簡單!
新聞熱點
疑難解答
圖片精選