十、jquery-ui datepicker实现选择一周的日期

html页面:

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/css/jquery-ui.css">
    <script type="text/javascript" src="/static/js/jquery-2.1.0.js"></script>
    <script type="text/javascript" src="/static/js/jquery-ui.js"></script>
    <title>查看已上线需求</title>
</head>

<body>
    <div class='mycontainer'>
        <table>
            <tr>
                <td>日期
                    <input id="dateWeekRange" type="text" class="datePicker input-sm" placeholder="选择时间段" />
                </td>
            </tr>
        </table>
        <br/>
        <div id="showChart"></div>
    </div>
</body>
</html>

javascript:

<script type="text/javascript">  
$(document).ready(function(e) {  
    var date = new Date();  
    var mondayDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 1);
    var sundayDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 7);

    var startDateStr = $.datepicker.formatDate('yy-mm-dd', mondayDate);
    var endDateStr = $.datepicker.formatDate('yy-mm-dd', sundayDate);
    $('#dateWeekRange').val(startDateStr + " ~ " + endDateStr);
});
var startDate;
var endDate;

jQuery(function($) {
    $.datepicker.regional['zh-CN'] = {
        firstDay: 1,
        initStatus: '请选择日期',
        isRTL: false
    };
    $.datepicker.setDefaults($.datepicker.regional['zh-CN']);
    
    var selectCurrentWeek = function() {
        window.setTimeout(function() {
            $('#dateWeekRange').find('.ui-datepicker-current-day a').addClass('ui-state-active')
        }, 1);
    }
    $('#dateWeekRange').datepicker({
        showOtherMonths: true,
        selectOtherMonths: true,
        onSelect: function(dateText, inst) {
            var date = $(this).datepicker('getDate');

            startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 1);
            endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 7);
            var dateFormat = inst.settings.dateFormat || $.datepicker._defaults.dateFormat;

            var startDateStr = $.datepicker.formatDate('yy-mm-dd', startDate, inst.settings);
            var endDateStr = $.datepicker.formatDate('yy-mm-dd', endDate, inst.settings);

            $('#dateWeekRange').val(startDateStr + " ~ " + endDateStr);
            selectCurrentWeek();
        },
        beforeShowDay: function(date) {
            var cssClass = '';
            if (date >= startDate && date <= endDate)
                cssClass = 'ui-datepicker-current-day';
            return [true, cssClass];
        },
        onChangeMonthYear: function(year, month, inst) {
            selectCurrentWeek();
        }
    });

    $(document).on("mousemove", ".ui-datepicker-calendar tr", function() {
        $(this).find('td a').addClass('ui-state-hover');
    });
    $(document).on("mouseleave", ".ui-datepicker-calendar tr", function() {
        $(this).find('td a').removeClass('ui-state-hover');
    });
});
</script>

效果如下:
输入图片说明

jQuery 1.7之后live()就被废弃了,用.on()替代

// Live
$( ".bookList li" ).live( "click", function( e ) {} );
$( document ).on( "click", ".bookList li", function( e ) {} );

参考文章:
jQuery API中文文档
jquery .bind vs .live

转载于:https://my.oschina.net/sshen11111/blog/1526033

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值