下拉列表(Asp.net Web 表单)未填充数据

我有 jQuery 调用来获取数据和 UI 上的下拉列表,它没有填充数据。我尝试了很多方法,评论的是我使用的代码。如果我在代码中做错了什么,请告诉我。


var questionData;

var optionData;


$(document).ready(function () {    

    $.ajax({

        url: 'coaching-assessment-tool.aspx/GetCATQuestionAndOptions',

        type: 'POST',

        dataType: 'json',

        contentType: "application/json; charset=utf-8",

        success: function (data) {            

            questionData = data.d[0];

            optionData = data.d[1];           

            console.log(questionData[0].QuestionText);

            console.log("Question Data", questionData);

            console.log("Option Data", optionData);

            //Questions

            document.getElementById('firstQuestion').innerHTML = questionData[0].QuestionText;

            document.getElementById('secondQuestion').innerHTML = questionData[1].QuestionText;

            document.getElementById('thirdQuestion').innerHTML = questionData[2].QuestionText;

            document.getElementById('fourthQuestion').innerHTML = questionData[3].QuestionText;

            document.getElementById('fifthQuestion').innerHTML = questionData[4].QuestionText;

            document.getElementById('sixthQuestion').innerHTML = questionData[5].QuestionText;

            document.getElementById('seventhQuestion').innerHTML = questionData[6].QuestionText;

            document.getElementById('eighthQuestion').innerHTML = questionData[7].QuestionText;

            document.getElementById('ninthQuestion').innerHTML = questionData[8].QuestionText;

            document.getElementById('tenthQuestion').innerHTML = questionData[9].QuestionText;

                });

            });            

        },



慕侠2389804
浏览 131回答 2
2回答

SMILET

当获取返回的数据时,您的数据返回处理程序有点关闭,您可以执行以下操作来缓解这种情况:function (data, status) {&nbsp; &nbsp; var json = data;&nbsp; &nbsp; obj = JSON.parse(json);&nbsp; &nbsp; var opt = null;&nbsp; &nbsp; $("#targetIDOfControl").empty();&nbsp; &nbsp; for (i = 0; i < obj.People.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (i < obj.People.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opt = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opt = document.createElement("option");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("targetIDOfControl").options.add(opt);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opt.text = obj.People[i].Name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opt.value = obj.People[i].ID;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }上面的例子创建了一个选项,然后设置它的文本和值。然后根据返回的 JSON 数组中的索引数量复制更多选项。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript