2017-02-10 142 views
1

我有一个引导模式只有一个“选择”字段,我用它来选择颜色或性别剪裁或大小为我的beckend网络上选定的服装产品应用程序。如何初始化(并重新初始化)通过JavaScript引导选择

我通过ajax获取数据,返回一个带有{Id,Text,Val}的json数组,用于颜色/性别或大小(id区分颜色/性别/大小),并在添加新选项之前清除我的选择。

这个作品很好地与标准的HTML选择..

<select id="productProperty" class="form-control" multiple></select> 

输出与标准选择:

output with standard select

但没有任何工程与自举选

<select id="productProperty" class="form-control selectpicker" multiple></select> 

输出自举选择:

[Output with bootstrap select[2]

这里是我的Ajax调用

 $.ajax({ url: 'theUrl', 
       type: "GET", 
       data: { requiredInput: 'that' } 
      }).done(function(response) { 

       $("#productProperty").empty(); 
       var selectHtml = ""; 
       $.each(response.data, function(index, item) { 
        selectHtml += "<option value='" + item.Id + "' " + disabled_ + ">" + item.Name + "</option>"; 
       }); 

       $("#productProperty").html(selectHtml); 
       $("#productProperty").selectpicker('refresh'); 

      }); 

我有我的布局(MVC)文件,因为我与日期选择做$( 'selectpicker')。selectpicker()和数据表

如何用bootstrap-select解决这个问题? 谢谢。

+0

难道你不想保持那种能够一次看到所有选项的选择,就像第一张图片中的选项一样吗?如果你想这样做,你不能使用引导选择插件 –

回答

0

为什么你不通过jQuery构建整个事物?

首先,选择下拉菜单:

$.ajax({ url: 'theUrl', 
      type: "GET", 
      data: { requiredInput: 'that' } 
     }).done(function(response) { 

      $.each(response.data, function(index, item) { 
       html += "<option value='" + item.Id + "' " + disabled_ + ">" + item.Name + "</option>"; 
      }); 
     }); 

然后关闭标签:

var html = ""; 
html += '<select id="productProperty" class="form-control selectpicker" multiple>'; 

然后通过你的Ajax去

html += "</select>"; 

追加它,你应该是所有好。

$(".container").append(html); 
+0

这样做,我这样做,但仍然不显示选项。相同的代码适用于标准选择(通过删除'selectpicker'类) – Mxo