在变更时填充下拉列表

时间:2015-01-30 05:55:47

标签: jquery

我编写下面的代码并且它工作正常它也会带来表中的数据,但它不会显示在随后的下拉列表中 这是我的代码

<script type = "text/javascript">
    var pageUrl = '<%=ResolveUrl("~/vendor_master.aspx")%>'
    function PopulateStates() {
        if ($('#<%=ddl_state.ClientID%>').val() == "0") {
            $('#<%=ddl_city.ClientID %>').empty().append('<option selected="selected" value="0">Please select</option>');
        }
        else {
            $('#<%=ddl_city.ClientID %>').empty().append('<option selected="selected" value="0">Please select</option>');
            $.ajax({
                type: "POST",
                url: pageUrl + '/PopulateCities',
                data: '{stateID: ' + $('#<%=ddl_state.ClientID%>').val() + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnCountriesPopulated,
                failure: function (response) {
                    alert(response.d);
                }
            });
        }
    }

    function OnCountriesPopulated(response) {
        PopulateControl(response.d, $("#<%=ddl_city.ClientID %>"));
    }
</script>

我在一个萤火虫中出现以下错误

ReferenceError: PopulateControl is not defined
    PopulateControl(response.d, $('#ddl_city'));

我删除了引号,但它对我不起作用 请建议我提前解决错误

1 个答案:

答案 0 :(得分:0)

<script type = "text/javascript">
    function PopulateControl(list, control) {
        if (list.length > 0) {
            control.removeAttr("disabled");
            control.empty().append('<option selected="selected" value="0">Please select</option>');
            $.each(list, function () {
                control.append($("<option></option>").val(this['Value']).html(this['Text']));
            });
        }
        else {
            control.empty().append('<option selected="selected" value="0">Not available<option>');
        }
    }
</script>