【问题标题】:nested selectbox - set second jquery dropdown when first is changed嵌套选择框 - 在第一个更改时设置第二个 jquery 下拉列表
【发布时间】:2014-11-25 00:29:34
【问题描述】:

我正在尝试使用 jQuery/AJAX 和 PHP/MySQL 创建一组动态的下拉框。当页面根据数据库中的值加载时,将填充第一个下拉框。第二个下拉框应根据第一个下拉框中的选择显示一组值。

我使用 Chosen 插件来获得搜索选项。

我的问题是:当我选择第一个选择框时,第二个选择框没有显示任何内容。当我从 Html 中删除 class="chosen-select" 时,它可以正常工作!

我需要在选择框中进行搜索。那么,我该怎么办?

<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
 $('.chosen-select').chosen({ 
         no_results_text: "Oops, nothing found!"
    });  
$('#search1').on('change', function (){
$('#search2').html("<option value=''>Select</option>");// add this on each call then add the options when data receives from the request
    $.getJSON('select.php', {id: $(this).val()}, function(data){
        var options = '';
        for (var x = 0; x < data.length; x++) {
            options += '<option value="' + data[x]['id'] + '">' + data[x]['Date'] + '</option>';
        }
        $('#search2').html(options);
        //$('.chosen-select').chosen();
    });
});
});
</script>

这是第二次选择:

<select  id="search2" name="search" type="text" data-placeholder="Choose an Option..."     style="width:370px;" class="chosen-select" onChange="drawChart(this.value);">
<option value=""></option> 
</select>

在此先感谢您。

【问题讨论】:

    标签: php jquery ajax drop-down-menu jquery-chosen


    【解决方案1】:

    终于找到答案了:)

    <script type="text/javascript" charset="utf-8">
    $(document).ready(function(){
       $('.chosen-select').chosen({
             no_results_text: "Oops, nothing found!"
        });  
    $('select#search1').on('change', function (){
        $('select#search2').html("<option value=''>Select the Option..</option>");// add this on each call then add the options when data receives from the request
        $.getJSON('select.php', {id: $(this).val()}, function(data){
            $('select#search2').empty(); 
    
            var options = '<option value="0">Select the Option..</option>';
            for (var x = 0; x < data.length; x++) {
                options += '<option value="' + data[x]['id'] + '">' + data[x]['Date'] + '</option>';
            }
            $('select#search2').chosen();
            $('select#search2').html(options);
            $("#search2").trigger("chosen:updated");
        });
    });
    });
    </script>
    

    【讨论】:

      猜你喜欢
      • 2013-06-28
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      相关资源
      最近更新 更多