简体   繁体   English

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

[英]Dynamically populate dropdown from database

I am looking to do this : 我正在寻找这样做:

I have two field 1. Date - using jquery datepicker 2. DropDown - Populated with student id 我有两个字段1.日期-使用jquery datepicker 2.下拉菜单-使用学生ID填充

So , when i select a date say 03/09/2012 i should get all student id present on that day (say 15 of them) , then when i change the date to 04/09/2012 i should get id of only student that are present on that day ( say 29 of them) 因此,当我选择一个日期说03/09/2012时,我应该得到当天的所有学生证(比如说其中的15个),那么当我将日期更改为04/09/2012时,我应该得到唯一的学生证那天在场(例如其中29场)

I have done is , i have made ajax call to query the database like : 我所做的是,我已经进行了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
});
});

No issues about getting the data, i just want to know how to populate the dropdown using the array(json) that i have 关于获取数据没有问题,我只想知道如何使用我拥有的array(json)填充下拉列表

Use this inside your ajax success function: 在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>'));
}

you can use datepicker onselect method 您可以使用datepicker onselect方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM