如何将数据绑定到bootstrap select

时间:2017-04-11 20:09:16

标签: jquery twitter-bootstrap

json数据:

["string 1","string 2","test 07 ","here is test"]

这是html

  <select class="selectpicker" id="ddl_location">
    <option select="selected">Select Location </option>
  </select>

这里是JQuery代码

 for (var i = 0; i < jsonData.length; i++) {
     $("#ddl_location").append('<option value="' + jsonData[i] + '">' + jsonData[i] + '</option>');
      }

问题在于

  

数据未在下拉列表中填充

3 个答案:

答案 0 :(得分:0)

代码似乎正常工作。

&#13;
&#13;
var jsonData = ["string 1", "string 2", "test 07 ", "here is test"];

for (var i = 0; i < jsonData.length; i++) {
  $("#ddl_location").append('<option value="' + jsonData[i] + '">' + jsonData[i] + '</option>');
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select class="selectpicker" id="ddl_location">
  <option select="selected">Select Location </option>
</select>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

解决了这个问题之后,解决方案是:

 var jsonData = JSON.stringify(data);
     $.each(JSON.parse(jsonData), function (idx, obj) {
        $("#ddl_location").append('<option value="' + obj.id + '">' + obj.location + '</option>').selectpicker('refresh');
     });

步骤1:将收到的对象字符串化 第2步:使用每个函数而不是for循环
第3步:使用JSON.parse(jsonData )解析数据 每次使用.selectpicker('refresh')

将数据附加到下拉列表时,请执行步骤4:刷新下拉列表

答案 2 :(得分:0)

记住要附加$ select.append(data).selectpicker('refresh');当您没有在下拉列表中填充数据时

相关问题