如何动态传递数据以选择列表?

时间:2019-06-11 05:41:06

标签: javascript c# jquery ajax

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

if errorlevel 1

我的选择列表

<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>

1 个答案:

答案 0 :(得分:0)

将您的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 this line if the data is not already json formatted
                    $("#Subaccount").empty();
                    selectData.forEach(function (obj) {
                    //NOTE the name Value and Text are what you assigned them when you fetched them
                        $('#Subaccount').append($('<option></option>').val(obj.Value).html(obj.Text));
                    });
                }
            });
        }
    });
});