【问题标题】:How to dynamicly pass data to select list?如何动态地将数据传递给选择列表?
【发布时间】:2019-10-25 11:44:48
【问题描述】:

我需要使用 jquery 将数据动态传递到我的选择列表。我在控制台中看到了该数据,但我的列表为空。你能帮我找到解决办法吗?

<script type="text/javascript">
    var kunnr;
    $(document).ready(function () {
        $('#NAME1').autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "Form2",
                    method: 'POST',
                    data: {
                        term: $('#NAME1').val()
                    },
                    success: function (data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                kunnr = ui.item.kunnr;
                $.ajax({
                    url: "Form3",
                    method: 'POST',
                    data: {
                        kunnr: kunnr
                    },
                    success: function (data) {
                        console.log(data); 
                    //there is my data ^ i need to pass to select list

                    }
                });
            }
        });
    });
</script>

我的选择列表

@Html.DropDownListFor(model => model.Subaccount, new SelectList(" "), new { @class = "form-control" })

【问题讨论】:

  • $.ajax({ url: "Form2", dataType:'json',

标签: javascript c# jquery ajax


【解决方案1】:

把你的javascript代码改成这个

var kunnr;
$(document).ready(function () {
    $('#NAME1').autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "Form2",
                method: 'POST',
                data: {
                    term: $('#NAME1').val()
                },
                success: function (data) {
                    response(data);
                }
            });
        },
        select: function (event, ui) {
            kunnr = ui.item.kunnr;
            $.ajax({
                url: "Form3",
                method: 'POST',
                data: {
                    kunnr: kunnr
                },
                success: function (data) {
                    var selectData = JSON.parse(data);//use JSON.parse(), if the data is not already json formatted
                    $("#Subaccount").empty();
                    selectData.forEach(function (obj) {
                    //NOTE the Value and Text are what you assigned them when you fetched them
                        $('#Subaccount').append($('<option></option>').val(obj.Value).html(obj.Text));
                    });
                }
            });
        }
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 2016-10-09
    • 2020-08-04
    • 1970-01-01
    • 2015-12-21
    • 2021-06-25
    • 2013-02-04
    相关资源
    最近更新 更多