2017-06-02 109 views
0

我在我看來填充DROPDOWNLIST

@Html.DropDownList("Question1", null, "Вопрос 1", htmlAttributes: new {@class = "form-control", @style = "height:40px;margin-bottom: 20px;"}) 

有下拉列表中我有AJAX調用顯示所有問題

function question_update() { 
    $(".count").empty(); 
    $.ajax({ 
     url: '@Url.Action("QuestionsList", "Questions")', 
     contentType: 'application/json; charset=utf-8', 
     type: 'GET', 
     dataType: 'json', 
     processData: false, 
     success: function (result) { 
      var email = result; 
      var edit = '@Url.Content("~/Images/Edit.png")'; 
      var delete_ = '@Url.Content("~/Images/Delete.png")'; 
      // console.log(result[0].Name); 
      for (var i = 0; i <= email.length - 1; i++) { 
       var arrow = '@Url.Content("~/Images/plus_plus.png")'; 
       var questionHtml = '<div class = "title" style="margin-top:15px;margin-left:15px;margin-bottom:10px;">' 
        + 
        '<img class="click" src="' 
        + arrow 
        + '">' + 
        '<span class="test">' + 
        '<input type="text" class="testclass" readonly value="' + 
        result[i].Quest + '">' + 
        '<a style="margin-left:25px;">' + 
        '<img src="' + edit + '"/>' + 
        '<img style="margin-left:10px;" src="' + delete_ + '"/>' + 
        '</div>' + 
        '<div class ="content" style="margin-left:60px; width: 80%; height: 100px; background: white;">' + 
        '<div style="width: 100%">' + 
        '<div style="float:left; width: 50%;">' + 
        '<b style="margin-left: 40px;">' + "Время на ответ" + '</b>' + 
        '<b>' + ":" + '</b>' + 
        '<span>' + "Время на подготовку" +'</span>'+ 
        '</div>' + 
        '<div style="float:right; width: 50%;">' + 
        '<b>' + result[i].TimeForAnswer + '</b>' + 
        '<b>' + ":" + '</b>' + 
        '<b>' + result[i].TimeForReady + '</b>'+ 
        '</div>' + 
        '</div>' + 
        '</div>'; 
       $(".count").append(questionHtml); 

在成功之處我需要更新下拉列表中的值

我就這樣做

function question_update() { 
    $(".count").empty(); 
    $.ajax({ 
     url: '@Url.Action("QuestionsList", "Questions")', 
     contentType: 'application/json; charset=utf-8', 
     type: 'GET', 
     dataType: 'json', 
     processData: false, 
     success: function (result) { 
      var email = result; 
      var edit = '@Url.Content("~/Images/Edit.png")'; 
      var delete_ = '@Url.Content("~/Images/Delete.png")'; 
      // console.log(result[0].Name); 
      for (var i = 0; i <= email.length - 1; i++) { 
       var arrow = '@Url.Content("~/Images/plus_plus.png")'; 
       var questionHtml = '<div class = "title" style="margin-top:15px;margin-left:15px;margin-bottom:10px;">' 
        + 
        '<img class="click" src="' 
        + arrow 
        + '">' + 
        '<span class="test">' + 
        '<input type="text" class="testclass" readonly value="' + 
        result[i].Quest + '">' + 
        '<a style="margin-left:25px;">' + 
        '<img src="' + edit + '"/>' + 
        '<img style="margin-left:10px;" src="' + delete_ + '"/>' + 
        '</div>' + 
        '<div class ="content" style="margin-left:60px; width: 80%; height: 100px; background: white;">' + 
        '<div style="width: 100%">' + 
        '<div style="float:left; width: 50%;">' + 
        '<b style="margin-left: 40px;">' + "Время на ответ" + '</b>' + 
        '<b>' + ":" + '</b>' + 
        '<span>' + "Время на подготовку" +'</span>'+ 
        '</div>' + 
        '<div style="float:right; width: 50%;">' + 
        '<b>' + result[i].TimeForAnswer + '</b>' + 
        '<b>' + ":" + '</b>' + 
        '<b>' + result[i].TimeForReady + '</b>'+ 
        '</div>' + 
        '</div>' + 
        '</div>'; 
       $(".count").append(questionHtml); 

       $.ajax({ 
        url: '@Url.Action(" QuestionsList_new", "Questions")', 
        contentType: 'application/json; charset=utf-8', 
        type: 'GET', 
        dataType: 'json', 
        processData: false, 
        success: function (result) { 
         var $vacancy = $("#Question1"); 
         $vacancy.empty(); 
         $.each(result, function (a, b) { 
          $vacancy.append('<option value="' + b.Value + '">' + b.Text + '</option>'); 
         }); 
        } 
       }); 
      } 
     } 
    }); 
} 

但我在DropdownList中看不到任何更新。

我的問題在哪裏?

UPDATE

所有好。但我有新問題。在我的下拉列表中,我有undefined

<select class="form-control" id="Question1" name="Question1" style="height:40px;margin-bottom: 20px;"><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option></select>

我該如何解決這個問題?

+1

嘗試使用'$ vacancy.append($(''));' –

+0

檢查$ vacancy是否存在Ajax成功後的DOM。 – User3250

+0

check dom我想知道$(「#Question1」)的值。innerHtml() –

回答

0

根據您所提供的result變量從服務器回來的樣本數據:

{ ID = 1048, Quest = "тест" } 

你的。每次循環需要看起來像這樣:

$.each(result, function (a, b) { 
    $vacancy.append('<option value="' + b.ID + '">' + b.Quest + '</option>'); 
}); 

屬性「文本」和你試圖訪問的「價值」在數據中不存在,因此你爲什麼變得「未定義」。您需要使用涉及的實際屬性的名稱。