在Ajax中没有填充下拉列表

时间:2018-03-06 09:51:43

标签: jquery ajax html-select

我有两个下拉菜单。第一个是RouteTypeSelect,第二个是RouteNameSelect

我希望使用Ajax填充第二个更改第一个以从数据库动态获取数据。我已经尝试了几种方法来填充下拉列表,所有这些都在Ajax帖子之外工作,但是当在Ajax的响应中设置时它不起作用:

var $select1 = $('#RouteTypeSelect'), $select2 = $('#RouteNameSelect'), $options = $select2.find('option');

$select1.on('change', function () {
    $.ajax({
        type: "POST",
        url: "page1.aspx/GetRouteName",
        data: '{IdRouteType: "3" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data2) {
            options = [];
            $.each($.parseJSON(data2.d), function () {
                var id= this.IdRoute;
                var name= this.Name;
                $("<option></option>", {
                    value: this.IdRoute,
                    text: this.Name
                }).appendTo($('#RouteNameSelect'));
            });
        },
        failure: function () {
            alert("Failed!");
        }
    });
}).trigger('change');

代码背后:

[WebMethod]
public static string GetRouteName(string IdRouteType)
{
    List<Route> RouteNameList2 = new List<Route>();
    RouteLogic RouteBLL = new RouteLogic();
    DataTable RouteTypeDT = RouteBLL.SelectRouteByIdRouteType(Convert.ToInt16(IdRouteType));
    for (int i = 0; i < RouteTypeDT.Rows.Count; i++)
    {
        Route g = new Route();
        g.IdRoute = Convert.ToInt32(RouteTypeDT.Rows[i]["IdRoute"]);
        g.Name = RouteTypeDT.Rows[i]["Name"].ToString();
        RouteNameList2.Add(g);
    }
    return JsonConvert.SerializeObject(RouteNameList2);
}

我试过这个@

$('#RouteNameSelect3232').append($("<option     />").val(this.IdRoute).text(this.Name));

而且:

options = [];
options.push('<option value="' + this.IdRoute + '">' + this.Name + '</option>');

而且:

$select2.html('<option value="' + this.IdRoute + '">' + this.Name + '</option>');

请注意this.Namethis.IdRoute正常填写

如何解决此问题?

更新:以下只附加最后一个值...我想附加一个选项列表。

$('#RouteNameSelect3232').val(null).trigger('change');
var studentSelect = $('#RouteNameSelect3232');
$.ajax({
    type: 'POST',
    url: 'AddBus.aspx/GetRouteName',
    contentType: "application/json; charset=utf-8",
    data: '{IdRouteType: "3" }'
}).then(function (data2) {

    // Create the option and append to Select2
    $.each($.parseJSON(data2.d), function () {
        var option = new Option(this.Name, this.IdRoute, true, true);
        studentSelect.append(option).trigger('change');
    }),

    // Manually trigger the `select2:select` event
    studentSelect.trigger({
        type: 'select2:select',
        params: {
            data: data2
        }
    });
});

2 个答案:

答案 0 :(得分:0)

试试吧......

$.each([{id:1,value:'hello'},{id:2,value:'hey'}], function () {
    $("<option></option>", {
        value: this.id,
        text: this.value
    }).appendTo($('#myselect'));
});

答案 1 :(得分:0)

在其中添加新的select2()

后,您需要在选择时运行<option>

检查这个小提琴 http://jsfiddle.net/4ad7A/963/

更新您的成功功能

     success: function (data2) {
                        options = [];
                        $.each($.parseJSON(data2.d), function () {
                            var id= this.IdRoute;
                            var name= this.Name;
$('#RouteNameSelect').append( $("<option>", {
                                value: id,
                                text: name
                            }));
                        });   
        $('#RouteNameSelect').select2()


                    },