ajax綁定靜態下拉式清單,級聯跳轉

來源:互聯網
上載者:User

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="CasMenu.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Porschev--前台JS(Jquery)調用後台方法 串聯功能表</title>

<script src="http://www.cnblogs.com/Javascript/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function() {
            $("#selectShop").change(function() {    //省份下拉式功能表的change事件
                var params = '{str:"' + $(this).val() + '"}';  //此處參數名要注意和後台方法參數名要一致              
                $.ajax({
                    type: "POST",                   //提交方式
                    url: "List.aspx/ShowShopPro",   //提交的頁面/方法名
                    data: params,                   //參數(如果沒有參數:null)
                    dataType: "text",               //類型
                    contentType: "application/json; charset=utf-8",
                    beforeSend: function(XMLHttpRequest) {
                        $('#tipsDiv').text("正在查詢...");
                    },
                    success: function(msg) {
                        $('#tipsDiv').text("查詢成功!");
                        $("#selectShopPro option").each(function() {
                            $(this).remove();   //移除原有項
                        });
                        $("<option value='0'>===請選擇===</option>").appendTo("#selectShopPro");   //添加一個預設項
                        $(eval("(" + msg + ")").d).appendTo("#selectShopPro");        //將返回來的項添加到下拉式功能表中
                    },
                    error: function(xhr, msg, e) {
                        alert("error");
                    }
                });
            });

        });
        var i = 1;

  //複製添加
        function adds() {
            $("#selectShop").clone().prependTo("#tipsDiv");
            $("#tipsDiv select").attr("id","select"+i+"");
            alert($("#tipsDiv").html());
            i++;
        }
    </script>

   </head>
<body>
    <form id="form1" runat="server">
    <div>
        Porschev--前台JS(Jquery)調用後台方法 串聯功能表<br />
        <br />
     <select id="selectShop">
                     <%=strShop%>
              </select>
                        
             <select id="selectShopPro" name="selCity">
                    <option value="0">===請選擇===</option>
             </select>
                        
           <div id="tipsDiv"></div>
           <a href="javascript:adds()">添加</a>

     </div>

    </form>
</body>
</html>
 

CasMenu.aspx.cs 檔案中

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using CasMenuModels;  
using CasMenuBLL;  
using System.Text;   
 
public partial class _Default : System.Web.UI.Page   
{     
    protected void Page_Load(object sender, EventArgs e)   
    {   
       if (!IsPostBack)   
        {   
                ShowPro ();
        }         
   }  

        public static string strShop = string.Empty;
        public static string strShopPro = string.Empty;

       public string ShowPro()
        {
            StringBuilder str = new StringBuilder();
            List<ZGWed.Model.Shop> list = new ShopBLL().GetALLItems();

            //添加一個初始項
            str.Append("<option value=\"");
            str.Append("0");
            str.Append("\">");
            str.Append("===請選擇===");
            str.Append("</option>");

            //迴圈追加省份下拉項
            foreach (ZGWed.Model.Shop s  in list)
            {
                str.Append("<option value=\"");
                str.Append(s.ID);
                str.Append("\">");
                str.Append(s.ShopName);
                str.Append("</option>");
            }
            return strShop = str.ToString();
        }

        [System.Web.Services.WebMethod()]
        public static string ShowShopPro(string str)
        {
            StringBuilder strCi = new StringBuilder();
            if (str == "0")     //為初始項
            {
                strCi.Append("<option value=\"");
                strCi.Append("請選擇");
                strCi.Append("\">");
                strCi.Append("請選擇");
                strCi.Append("</option>");
            }
            else
            {
                List<string> scon = new List<string>();
                scon.Add("shopid,"+str+",=");
                List<ZGWed.Model.ShopProductInfo> list = new ShopProductInfoBLL().GetItemsByCondition(scon) ;
                foreach (ZGWed.Model.ShopProductInfo c in list)
                {
                    strCi.Append("<option value=\"");
                    strCi.Append(c.ID);
                    strCi.Append("\">");
                    strCi.Append(c.ShopID);
                    strCi.Append("</option>");
                }
            }
            return strShopPro = strCi.ToString();
        }

  #endregion   
     

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/porschev/archive/2010/10/15/5943579.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.