仅使用onChangeMonthYear选择年份来显示数据

时间:2013-08-29 14:15:21

标签: jquery datepicker onselect

我目前正在做一个项目,它将显示需要yy-mm-dd的日常数据(例如:2013-06-01),需要yy-mm的月度数据(例如:2013-06)和年度数据只需要一年(例如:2013年)。我的问题是:

  1. 当您选择一年(ui-datepicker-year)时,它应根据所选年份显示全年的数据(例如,选定年份为2013年。因此它应显示2013-01至2013年的数据 - 12)。
  2. 所以这是 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);
                }
            });
        }));
    }
    

    另外我尝试在if.etp中的​​ if($ _ POST ['type'] ==“month”)*之后添加其他条件:

    else if($_POST['type']=="year") {
        $where = "YEAR(dbDate) = '{$_POST['year']}'";
    }
    

0 个答案:

没有答案