基於jquery ajax實現DropDownList二級聯動

來源:互聯網
上載者:User

這節主要內容是通過AJAX調用頁面後台代碼方法實現下拉框二級聯動效果,實現步驟如下:

1.建立檔案Recipe24.aspx,實現後台代碼如下:

// 引入命名空間

 代碼如下 複製代碼

using System.Web.Services;
// 實現下拉框二級聯動AJAX請求載入資料方法
    [WebMethod()]
    public static ArrayList GetSubList(string sBuyID)
    {
        ArrayList subList = new ArrayList();

        if (sBuyID == "1")
        {
            subList.Add(new ListItem("文藝", "1"));
            subList.Add(new ListItem("少兒", "2"));
            subList.Add(new ListItem("人文社科", "3"));
            subList.Add(new ListItem("科技", "4"));
        }
        else if (sBuyID == "2")
        {
            subList.Add(new ListItem("手機通訊", "1"));
            subList.Add(new ListItem("手機配件", "2"));
            subList.Add(new ListItem("攝影攝像", "3"));
            subList.Add(new ListItem("數位配件", "4"));
        }

        return subList;
    }

2.實現頁面代碼(HTML部分)如下:

 代碼如下 複製代碼
<body>
    <form id="form1" runat="server">
    <div align="center">
        <fieldset style="width: 400px; height: 150px;">
            <table border="0" cellpadding="10" cellspacing="10">
                <tr>
                    <td>
                        <asp:DropDownList ID="buyList" runat="server" Width="120px">
                            <asp:ListItem Value="0" Text="  ---  請選擇  ---  "></asp:ListItem>
                            <asp:ListItem Value="1" Text="圖書"></asp:ListItem>
                            <asp:ListItem Value="2" Text="手機數位"></asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <td>
                        <asp:DropDownList ID="subList" runat="server" Width="120px">
                            <asp:ListItem Value="0" Text="  ---  請選擇  ---  "></asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
            </table>
        </fieldset>
    </div>
    </form>
</body>

3.實現指令碼代碼如下:

 代碼如下 複製代碼

<script type="text/javascript">
        $(function () {
            $("#buyList").bind("keyup change", function (e) {
                e.preventDefault();
                // 首先初始化
                $("#subList").empty().append($("<option></option>").val("0").html("  ---  請選擇  ---  "));
                if ($(this).val() != "0") {
                    sendData($(this).val());
                }
            });

            function sendData(sBuyID) {
                var loc = window.location.href;
                $.ajax({
                    type: "POST",
                    url: loc + "/GetSubList", // 調動後台頁面方法
                    data: '{"sBuyID":"' + sBuyID + '"}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        // msg.d是數組,由後台數組ArrayList返回,因此可以遍曆每個元素
                        $.each(msg.d, function () {
                            // this.Value和this.Text是後台返回數組ArrayList類型包含元素ListItem類型的屬性
                            $("#subList").append($("<option></option").val(this.Value).html(this.Text));
                        });
                    },
                    error: function () {
                        alert("ajax請求發生錯誤");
                    }
                });
            }
        });
    </script>

4.下拉框二級聯動效果圖:

5.分析XmlHttpRequest對象,可看到請求響應的資料msg.d的結構如下(通過下圖就知道msg.d的每個元素為什麼會有Text和Value屬性了):

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.