twitter-bootstrap 通过 Ajax 帖子加载 Bootstrap 选择选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23399676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 20:38:00  来源:igfitidea点击:

Loading Bootstrap select options through Ajax post

jqueryajaxtwitter-bootstraptwitter-bootstrap-3bootstrap-select

提问by Mona Coder

Using Bootstrap 3 and Bootstrap-Select plugin, I am trying to load the options of the select drop down by ajax from database.

使用 Bootstrap 3 和 Bootstrap-Select 插件,我试图从数据库中通过 ajax 加载选择下拉列表的选项。

The code works perfectly when I remove the select pickerclass from the selectelement but with that class (which is required to use Bootstrap select) it is not loading any of the options.

当我select pickerselect元素中删除类时,代码运行良好,但使用该类(使用 Bootstrap select 所需)时,它没有加载任何选项。

 <select id="arearesults" class="selectpicker" data-live-search="true">
    <option value="select">Select From The List</option>
 </select>

$('.btn-group .btn').click(function(){
    $.post(
    'con/animalList.php',
    { selType : this.id },
    function(data)
                {
                  $('#arearesults').html(data);
                }
    );
});

I already test the Bootstrap select plugin and it is also working in stand alone mode (hard coded). How can I solve this issue?

我已经测试了 Bootstrap 选择插件,它也在独立模式下工作(硬编码)。我该如何解决这个问题?

回答by Coderaemon

Multiple errors in your code.

您的代码中存在多个错误。

  1. You haven't mentioned the response data-typein the $.post. Is the response data "JSON"or "html", etc?

  2. After filling the values through AJAX in the selectbox you have to refresh it. Code:

    $('#arearesults').selectpicker("refresh");
    
  1. 您还没有data-type$.post. 响应数据是"JSON"还是"html"等?

  2. 在选择框中通过 AJAX 填充值后,您必须刷新它。代码:

    $('#arearesults').selectpicker("refresh");
    

回答by Muhammad

this solution worked for me

这个解决方案对我有用

  $('.selectpicker').selectpicker({});