使用Jquery在asp.net中级联下拉列表

时间:2012-09-21 01:22:23

标签: c# javascript jquery asp.net json

我在asp.net C#中的DropdownList中遇到问题,当我在bankDDL中选择一个银行时,branchDDL不会自动更新,需要在更新branchDDL之前先选择它。我使用Jquery语言在运行时填充我的branchDDL上的数据。

var branch = $("#<%=cboBranch.ClientID%>");

    $("#<%=cboBank.ClientID%>").change(function () {

        branch.html("");//this is my first problem this doesnt show after my bank is change
        branch.append($("<option></option>").val(-1).html("Please select Branch"));//also this
        if ($(this).val() != -1) {
            OnGetNotes(parseInt($(this).val()));//this function get the JSON and populate the
                                                //branch according to what bank is selected
                                                //and it show the branch using slideDown 
        }
        else {
            $("#branch").slideUp();
        }
    });

1 个答案:

答案 0 :(得分:1)

而不是.html()使用.empty()。 还要确保你的代码在DOM ready事件中。试试这个

$(function() {

    var branch = $("#<%=cboBranch.ClientID%>");

    $("#<%=cboBank.ClientID%>").on('change', function() {
        branch.empty().append($("<option></option>").val(-1).html("Please select Branch")); //also this
        if ($(this).val() != -1) {
            OnGetNotes(parseInt($(this).val()));
        }
        else {
            $("#branch").slideUp();
        }
    });
});​
相关问题