动态填充数据库中的下拉列表

时间:2012-09-03 06:32:36

标签: php jquery ajax drop-down-menu

我希望这样做:

我有两个领域 1.日期 - 使用jquery datepicker 2. DropDown - 填充学生ID

所以,当我选择一个日期说03/09/2012我应该在当天获得所有学生ID(比如15个),然后当我将日期更改为04/09/2012时我应该得到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)填充下拉列表

2 个答案:

答案 0 :(得分:2)

在你的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>'));
}

答案 1 :(得分:1)

您可以使用datepicker onselect方法

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