使用jquery JSON Handler实现级联效果

前台:

<script>
        $(document).ready(function(){

          

           //第一个下拉框的改变事件
            $("#ddlType").change(function(){

                 //调用Handler时需传递一个随机参数,告诉浏览器这是一个新请求,以免调用缓存中的Handler中存储的类型,以至于其他页面中添加的新类型不能显示出来。

                jQuery.getJSON("../ProductTypeHandler.ashx?w="+a,{"tid":$(this).val(),"r":Math.random()},function(date){
               
                     $("#ddlXiaoType").children().remove();
                    for(var i in date)
                    {
                        $("#ddlXiaoType").append("<option value='"+date[i].tid+"'>"+date[i].name+"</option>");
                    }
                    
                  });
            });

        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align:left;">
        <table width="60%" style="font-size:12px;border-collapse: collapse; text-align:left" border="1px"  cellpadding="0" cellspacing="0"  bordercolor="#C0C0C0">
         <tr><td style="text-align: center; height:20px; background-color:#CCCCCC " colspan="2" >添加产品</td></tr>
              <tr>

                  <td style="text-align: right">类型:</td><td>
                       <asp:DropDownList ID="ddlType" runat="server" Width="129px">
                       </asp:DropDownList>
                       <asp:DropDownList ID="ddlXiaoType" runat="server" Width="108px">
                       </asp:DropDownList>

                 </td>

                </tr>
        </table>
    </div>
    </form>
</body>

 

 

ProductTypeHandler.ashx中的定义:

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int tid = Convert.ToInt32(context.Request.Params["tid"]);
            List<cl.Model.Product_Type> newsList = new cl.BLL.gu.Product_Type().GetList(tid);//此处调用后台方法

            string locationString = "";
            locationString += "[";
            for (int i = 0; i < newsList.Count; i++)
            {
                locationString += "{/"tid/":/"" + newsList[i].t_id + "/",/"name/":/"" + newsList[i].t_name + "/"}";
                if (i < newsList.Count - 1)
                    locationString += ",";
            }
            locationString += "]";
            context.Response.Write(locationString);//返回json字符串
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值