2014-11-25 63 views
1

我想创建一个使用jQuery/AJAX和PHP/MySQL的动态下拉框设置。当页面根据数据库中的值加载时,第一个下拉框将被填充。第二个下拉框应显示一组基于来自第一个下拉框的选择的值。嵌套选择框 - 第一次更改时设置第二个jQuery下拉

我使用了选择插件为了有搜索选项。

我的问题是:当我选择第一个选择框时,第二个选择框中不显示任何内容。当我从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> 

预先感谢您。

回答

0

最后,我找到了答案:)

<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> 
相关问题