2016-07-05 38 views
0

我使用这个图片选择器jQuery插件(http://rvera.github.io/image-picker/),我面临着以下问题:重新填充图片选择器来选择控制与Ajax调用后,新的数据

Ajax调用后,重新填充用新数据选择控制。

$.ajax({ 
    type: "GET", 
    url: requestURL, 
    dataType: 'json', 
    success: function(result) 
    { 
     $.each(result, function(i, obj) { 
     console.log(obj.description); 
     $('#cake-filling-types-select') 
      .append($("<option data-img-src='" + obj.image + "'></option>")           
      .attr("value", obj.code) 
      .text(obj.description)); 
    }); 
    $("#cake-filling-types-select").imagepicker({ 
     show_label : true 
    }); 

    $("#cake-filling-types-select").data("picker").sync_picker_with_select(); 
}}); 

欲了解更多详情,请在这里找到问题:https://github.com/rvera/image-picker/issues/79

感谢。

回答

0

问题已解决。在@ArmindoMaurits @ GitHub的帮助下,使用'$(“#cake-filling-types-select”)。empty();'清理这个插件选择很好。

此外,对于其他问题,其中一些项目没有进入选择控制,我发现这些是缺少img src。

更新工作代码:

 `$.ajax({ 
      type: "GET", 
      url: requestURL, 
      dataType: 'json', 
      success: function(result){ 

       $("#cake-filling-types-select").empty(); 

       $.each(result, function(i, obj) { 

        if(obj.image != null) 
        { 
         var imgSrc = obj.image; 
        }else{ 
         imgSrc = ''; 
        } 

      $('#cake-filling-types-select') 
       .append($("<option data-img-src='" + imgSrc + "'></option>") 
         .attr("value", obj.code) 
         .text(obj.description)); 
       }); 

       $("#cake-filling-types-select").imagepicker({ 
        show_label : true 
       }); 

      }});`