从数据库动态填充下拉列表


Dynamically populate dropdown from database

我想做这件事:

我有两个领域1.日期-使用jquery日期选择器2.DropDown-使用学生id 填充

因此,当我选择一个日期,比如2012年9月3日时,我应该得到当天在场的所有学生id(比如其中15人),然后当我将日期更改为2012年09月4日时,我们应该只得到当天在场学生的id(比如29人)

我做的是,我做了ajax调用来查询数据库,比如:

$('#date_selected').blur(function(){
$.ajax({
//query database and get the array of student id present on that date
//but from jquery how do i know populate my dropdown ?
//i would probably get the results json encoded
});
});

获取数据没有问题,我只想知道如何使用

的数组(json)填充下拉列表

在ajax成功函数中使用此函数:

data = $.parseJSON(data);
var select_fields = data.selectf;
var select_id = "/*ID OF SELECT FIELD*/";
$('#'+select_id+' option').remove();
for(var x  in select_fields){
    $('#'+select_id).append($('<option value="'+select_fields[x]['value']+'">'+select_fields[x]['name']+'</option>'));
}

您可以使用日期选择器onselect方法

 $('#date_selected').datepicker({
    onSelect:function (selectedDate) {
       //make your jquery ajax post here
    }
   });