asp.net中js+jquery添加下拉框值和后台获取

简介:                     $(function () {             $(".cg2").change(function () {                 var id = $(this).

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $(".cg2").change(function () {
                var id = $(this).attr("id");
                var value = $(this).val();
                var newid = '#'+id.replace('_1_', '_2_');//把第一列id替换成第二列id
                //alert(newid);
                var data = "t1*v1|t2*v2|t3*v3";//后台获取的数据格式,这里用固定值了
                var datas = data.split('|');//分割成多组
                for (var i = 0; i < datas.length; i++) {
                    var d1 = datas[i].split('*');//每组分割成 显示值和真实值
                    $(newid).append("<option value=\""+d1[1]+"\">" + d1[0] + "</option>");
                    //alert(d1);
                }
               // alert(id + "|||" + value);
            });
        })
       
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>为了满足两列,任意多行。后台动态生成下拉框,还要前后列联级的需求。使用js+jquery,用服务器控件+Ajax也不行,老是选择之后就
        <asp:DropDownList ID="ddl_1_1" CssClass="cg2" runat="server">
        <asp:ListItem Text="txt1" Value="val1"></asp:ListItem>
        <asp:ListItem Text="txt1" Value="val1"></asp:ListItem>
        <asp:ListItem Text="txt1" Value="val1"></asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="ddl_2_1" runat="server">
        </asp:DropDownList><br/>
        <asp:DropDownList ID="ddl_1_2" CssClass="cg2"  runat="server">
        <asp:ListItem Text="txt2" Value="val2"></asp:ListItem>
        <asp:ListItem Text="txt2" Value="val2"></asp:ListItem>
        <asp:ListItem Text="txt2" Value="val2"></asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="ddl_2_2" runat="server">
        </asp:DropDownList><br/>
        <asp:Button ID="ButtonGet" runat="server" Text="获取" onclick="ButtonGet_Click" />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

 

//后台

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        protected void ButtonGet_Click(object sender, EventArgs e)
        {
            ///获取通过js加选择的 ddl_2_1 控件选中的值,显示在Label1上。
            Label1.Text = Request["ddl_2_1"].ToString();
        }

 

相关文章
|
2月前
|
开发框架 前端开发 JavaScript
使用JavaScript、jQuery和Bootstrap构建待办事项应用
使用JavaScript、jQuery和Bootstrap构建待办事项应用
15 0
|
4月前
|
开发框架 JSON JavaScript
ASP.NET Core3.1实战教程---基于Jquery单文件上传
ASP.NET Core3.1实战教程---基于Jquery单文件上传
26 0
|
5天前
|
JavaScript 前端开发 C++
【Web 前端】JavaScript window.onload 事件和 jQuery ready 函数有何不同?
【5月更文挑战第2天】【Web 前端】JavaScript window.onload 事件和 jQuery ready 函数有何不同?
|
20天前
|
JavaScript 前端开发 API
JavaScript和jQuery的区别
JavaScript和jQuery的区别
|
21天前
|
JavaScript 前端开发 API
js和jquery的区别
js和jquery的区别
16 8
|
26天前
|
JavaScript 前端开发
JQuery和JS的区别有哪些?
JQuery和JS的区别有哪些?
14 0
|
26天前
|
JavaScript 前端开发 API
JQuery的attr()属性和JS的属性的方法
JQuery的attr()属性和JS的属性的方法
10 0
|
1月前
|
JavaScript 前端开发 UED
|
4月前
|
XML JavaScript 前端开发
JavaScript学习 -- jQuery库
JavaScript学习 -- jQuery库
32 0
|
4月前
|
JavaScript
jQuery事件2-第6次课-给未来的元素绑事件-事件对象-JS盒模型-附案例、任务
jQuery事件2-第6次课-给未来的元素绑事件-事件对象-JS盒模型-附案例、任务
42 0