Javascript获取DropDownList选项信息

这是一个asp.net普通javascript的练习,是使用javascript去获取DropDownList Selected 选项的信息,如text,value和index等。

Insus.NET在.aspx.cs内准备好一些数据,是有关浏览器相关信息的,并写在泛型Dictionary<T,V>:

复制代码
 private Dictionary<string, string> GetBrowser()
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        dict.Add("google", "chrome");
        dict.Add("Mozilla", "firefox");
        dict.Add("microsoft", "ie");
        dict.Add("opera", "opera");
        dict.Add("safari", "apple");
        return dict;
    }
View Code
复制代码


在网页中,拉(PULL)DropDownList控件入网页.aspx中。

 <asp:DropDownList ID="DropDownList1" runat="server">               
            </asp:DropDownList>
View Code


去.aspx.cs为DropDownList控件绑定数据:



写Javascript脚本:

复制代码
function GetSelectedInfo() {
            var ddl = document.getElementById('<% =DropDownList1.ClientID %>');
            var info = "      The value is: " + ddl.options[ddl.selectedIndex].value + ".\n\r                text is: " +
                ddl.options[ddl.selectedIndex].text + ".\n\r option index is: " +
                ddl.selectedIndex;
            alert(info);
        }
View Code
复制代码


为了能执行上面的javascript脚本,Insus.NET在网页拉一个Button,然后让这个Button 执行一个OnClientClick客户端事件。

 <asp:Button ID="Button1" runat="server" Text="Get info" OnClientClick="GetSelectedInfo();" />


OK,试运行看看:

 

posted @   Insus.NET  阅读(1201)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2011-05-28 Gridview前面10行数据显示背景色
2011-05-28 为用户控件(UserControl)写属性
2011-05-28 Reset Image Size 2
点击右上角即可分享
微信分享提示