2011-09-20 106 views
0

我有一个页面,我显示一个jQuery日期选择器与一些突出显示的日期。如何根据日期选择器中单击的日期检索数据并填充div。这是我到目前为止的代码:如何根据DatePicker选择从Mysql检索数据?

<!DOCTYPE html> 
<html> 
<head> 
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> 


<script type="text/javascript"> 

var dates = [new Date(2011, 9-1, 20), new Date(2011, 9-1, 21),new Date(2011, 10-1, 1)]; 

$(function(){ 

    $('#datepicker').datepicker({ 
     numberOfMonths: [1,1], 
     dateFormat: 'dd/mm/yy', 
     beforeShowDay: highlightDays 
    }); 

    function highlightDays(date) { 
     for (var i = 0; i < dates.length; i++) { 
      if (dates[i].getTime() == date.getTime()) { 
       return [true, 'highlight']; 
      } 
     } 
     return [true, '']; 
    } 

}); 
</script> 

<style> 

#highlight, .highlight { 
    background-color: #cc0000; 
} 

</style> 


</head> 
<body style="font-size:62.5%;"> 

<div id="datepicker"></div> 

<div id="events"> 
Data From Mysql To Go Here 
</div> 
</body> 
</html> 

回答

1

好了,你需要做一个AJAX调用服务器端的一个脚本(PHP或asp.net或您选择的任何服务器端语言/框架)

编辑后看到你想要的代码在选择日期发生,我改变了JavaScript的一个小末编辑

这样的:

//add the onSelect function to your date picker, and put the code to fetch your events in there 
$('#datepicker').datepicker({ 
    numberOfMonths: [1,1], 
    dateFormat: 'dd/mm/yy', 
    beforeShowDay: highlightDays, 
    onSelect: function(date){ 
    // put your selected date into the data object 
    var data = {'date': date}; 

    // do the ajax call to the serverside script and pass 'data' to it. 
    $.get("serverurl/data/getdata.php", data, function(data){ 
     // this function is called upon successfully completing the ajax call 
     //do your magic here to put the data elements into your div, see below for an example 
     data.Events.each(function(d){ 
     var e = $('<div/>').text(d.Title); 
     $('#events').append(e); 
     }); 
    }); 
    } 
}); 

但这会要求你有你的访问getdata.php回到这样正确的JSON:

{ 
    Events: [ { 
     Title: "event 1", 
     Description: "event description" 
    }, 
    { 
     Title: "event 2", 
     Description: "event description" 
    } ] 
} 
+0

丢在这里我有点#键?点击日期选择器中的日期时会自动给出按钮的ID? – user875293

+0

我改变了代码,我以为你会有一个按钮,但你想要它在选择日期后立即,所以我用的选择事件的日期选择器 – Sander

+0

我已经想通了,但我不能发布我的工作了6几个小时这样生病试着发布我明天工作和想要的东西。谢谢你的帮助 – user875293

相关问题