我目前正在做一个项目,该项目将显示需要 yy-mm-dd 的每日数据(例如:2013-06-01)、需要 yy-mm 的每月数据(例如:2013-06)和只需要的年度数据年份(例如:2013)。我的问题是:
- 当你选择一个年份(ui-datepicker-year)时,它应该根据选择的年份显示全年的数据(例如,选择的年份是 2013。所以它应该显示从 2013-01 到 2013-12 的数据)。
所以这是jquery:
<script>
$(function() {
$("#datepicker").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd MM yy',
//SELECTED DATE: 2013-06-01
onSelect: function(dateText, inst) {
$.ajax({
type: "POST",
url: "content.php",
data: { "datepick" : dateText, "type" : "all"},
success: function(html) {
$("#content").empty().append(html);
}
});
},
onChangeMonthYear: function(year, month, inst){
$.ajax({
type: "POST",
url: "content.php",
data: { "datepick" : month, "type" : "month", "year" : year},
success: function(html) {
$("#content").empty().append(html);
}
});
}
});
});
</script>
<div id="datepicker"> </div>
这是php 文件(content.php):
if(isset($_POST['datepick']) && !empty($_POST['datepick'])) {
if($_POST['type']=="month") {
$where = "MONTH(dbDate) = '{$_POST['datepick']}' AND YEAR(dbDate) = '{$_POST['year']}'";
}
else {
$date = date_create($_POST['datepick']);
$calendarDate = date_format($date, 'Y-m-d');
$where = "DATE(dbDate) = '{$calendarDate}'";
}
}
else {
$calendarDate = date('Y-m-d');
$where = "DATE(dbDate) = '{$calendarDate}'";
}
$sql = "SELECT name FROM db WHERE {$where};
while($row = mysql_fetch_array($query))
{
$name = $row['name'];
}
但我不知道如何使用年度数据。我尝试对脚本使用 if-else 条件,但不起作用:
onChangeMonthYear: function(year, month, inst){
if($('.ui-datepicker-month :selected').click(function(){
var month = $('.ui-datepicker-month :selected').val();
var year = $('.ui-datepicker-year :selected').val();
$.ajax({
type: "POST",
url: "content.php",
data: { "datepick" : month, "type" : "month", "year" : year},
success: function(html) {
$("#content").empty().append(html);
}
});
}));
else if($('.ui-datepicker-year :selected').click(function(){
var year = $('.ui-datepicker-year :selected').val();
$.ajax({
type: "POST",
url: "content.php",
data: { "datepick" : year, "type" : "year"},
success: function(html) {
$("#content").empty().append(html);
}
});
}));
}
我还尝试在 content.php 中的 *if($_POST['type']=="month")* 之后添加这个 else if 条件:
else if($_POST['type']=="year") {
$where = "YEAR(dbDate) = '{$_POST['year']}'";
}