【问题标题】:How to retrieve Data from Mysql based on DatePicker Selection?如何根据 DatePicker 选择从 Mysql 中检索数据?
【发布时间】:2011-11-21 04:29:15
【问题描述】:

我有一个页面,其中显示了一个带有一些突出显示日期的 jquery 日期选择器。如何根据在日期选择器中单击的日期检索数据并填充 div。到目前为止,这是我的代码:

<!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>

【问题讨论】:

    标签: jquery datepicker


    【解决方案1】:

    好吧,您需要对服务器端脚本(php 或 asp.net 或您选择的任何服务器端语言/框架)进行 ajax 调用

    编辑在看到您希望代码在选择日期时发生后,我稍微更改了 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"
        } ]
    }
    

    【讨论】:

    • 在这里让我有点迷失#button ?点击日期选择器中的日期是否会自动给定按钮 ID?
    • 我更改了代码,我以为你会有一个按钮,但你想要在选择日期后立即使用它,所以我使用了 datepicker 的 onSelect 事件
    • 我已经弄明白了,但我无法发布我 6 小时的工作成果,所以我明天尝试发布我正在工作和想要的内容。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 2017-10-14
    相关资源
    最近更新 更多