2016-05-17 116 views
0

引導,選擇DropDownList的我有一個自舉選組合框,我在一個Ajax調用填充數據 HTML:沒有顯示

<div class="row"> 
<select id="selectGemeente1" class="selectpicker" data-live-search="true"></select> 
</div> 

Ajax調用:

var gemeenteLijst = []; 

var GetGemeentes = function() { 
    $.ajax({ 
     type: "GET", 
     dataType: "json", 
     url: 'https://localhost::blabla', 
     contentType: "application/json; charset=utf-8", 
     success: function (data) { 
      var test = document.getElementById("selectGemeente1"); 
      data.forEach(function (item) { 
       var opt = document.createElement("option"); 
       opt.value = item.GemeenteId; 
       opt.innerHTML = item.Naam; 
       test.appendChild(opt); 
      });   
     }, 
     error: function (xhr, status, error) { 
      alert(xhr.responseText); 
     } 
    }); 
} 

$(document).ready(function() { 
    GetGemeentes(); 
}) 

後運行我的應用程序,我的選擇已滿,但下拉列表未打開..

html after run

我已經看到了很多的解決方案的話,我應該(文件)把這個在我的$。就緒

$(".selectpicker").html(optgroup); 

但後來我得到一個錯誤說.selectpicker不是一個函數。

gemeenteLijstLaden.js:36 Uncaught TypeError: $(...).selectpicker is not a function 

我已經實現了這些腳本文件

<script src="~/Scripts/jquery-2.2.3.min.js"></script> 
<script src="~/Scripts/bootstrap.min.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/i18n/defaults-nl_NL.min.js"></script> 
<script src="~/Scripts/myCharts/gemeenteLijstLaden.js"></script> 
+0

你能提供jsfiddle嗎? – osmanraifgunes

+0

https://jsfiddle.net/vefnjdL2/ – willemH

回答

0
success: function (data) { 
    var selectGemeent = $("#selectGemeente1"); 
       selectGemeent.empty(); 
       $('#selectGemeente1').append($("<option></option>"). 
           attr("value", "0"). 
           text("Select")); 
       $.each(data.forEach, function (index, item) { 
        selectGemeent.append($('<option>', { 
         value: item.GemeenteId, 
         text: item.Naam 
        })); ////function 
       }); 
}, 

用它,在你成功的阿賈克斯和文本和值

+0

這隻會增加而不是我的其他選項。 – willemH

+0

如果它返回json 像這樣 return Json(new {forEach = yourFilledDDLName},JsonRequestBehavior.AllowGet); 然後這段代碼在這裏工作我不知道你在C#中的方法返回類型請確保你的方法在json中回調數據 – Vishal

3

我找到了解決辦法後添加其他的Fileds。 我只需要刷新我的選擇

$('.selectpicker').selectpicker('refresh');