json输出分类(js动态填充下拉框的选项)

本文介绍如何利用服务器端的cs代码生成ASHX文件返回JSON数据,然后在客户端使用JavaScript解析JSON并动态填充到下拉选择框中,涉及json、function、url和字符串操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一部分:cs代码,文件类型ashx 

//json输出 链接的分类
                    BLL_WebSiteGroupInfo bll_WebsiteGroupinfo = new BLL_WebSiteGroupInfo();
                    DataTable dt = bll_WebsiteGroupinfo.SelectWebGroupByUserId(userId);
                    StringBuilder builder = null;
                    builder = new StringBuilder();
                    builder.Append("[");
                    builder.Append("{groupId:1,groupName:'请选择'},");
                    builder.Append("{groupId:1,groupName:'未分类'},");
                    builder.Append("{groupId:2,groupName:'常用网址'},");
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            builder.Append("{");
                            builder.AppendFormat("groupId:" + dt.Rows[i]["GroupId"] + ",groupName:'" + dt.Rows[i]["Name"]+"'");
                            builder.Append("}");
                            builder.Append(",");
                        }
                    }
                    builder.Remove(builder.Length - 1, 1);
                    builder.Append("]");
                    string str = "{'group':" + builder + ",status:'true',type:'logined'}";
GoSuccess(str, 0);


public static void GoSuccess(string message, int gotype)
        {
            if (0 == gotype)
            {
                HttpContext.Current.Response.Write(message);
                HttpContext.Current.Response.End();
            }
            else
            {
                HttpContext.Current.Response.Write("<script>alert('" + message + "');history.back(-1)</script>");
                HttpContext.Current.Response.End();
            }
        }


第二部分:js代码

$(document).ready(function(){  
        $.ajax({  
           type: "POST",  
           url: "../MyLinks/HandlerForCollecting.ashx?title="+escape(title)+"&url="+url+"&name="+name+"&pwd="+pwd,
           data: "value=ajax",  
           success: function(msg){  
           var url= eval("("+msg+")");// *把JSON格式的字符串转换成js对象
           setCookie("GroupList",msg);
           
            var key, counter = 0;
            for(key in url.group) counter++;
          
             //解析json,添加 分类(json:登录状态,消息,分类集合)
             for(var i=0;i<counter;i++)
             {
                var valueItem=new Option(url.group[i].groupName,url.group[i].groupId);
                document.getElementById("ddlGroup").options.add(valueItem); 
             }
             
             if(url.status=="true")//msg=="登录成功!"
             {
                alert("登录成功!");
             
                setCookie("Username",document.getElementById('txtUsername').value);
                setCookie("Password",document.getElementById('txtPassword').value);
                
                document.getElementById("txtTitle").value=unescape(request('title'));
                document.getElementById("txtUrl").value=request('titleurl');
                document.getElementById("login").style.display="none";
                document.getElementById("collect").style.display="block";
             }
             if(url.status=="faild")
             {
                 alert("用户名或密码错误!");
                 return;
             }
           }
        });  
    }); 
    }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值