jQuery UI Datepicker - 多个日期选择

标签 jquery jquery-ui datepicker

有没有办法使用 jQuery UI Datepicker 小部件来选择多个日期?

感谢所有帮助! 如果无法使用 jquery UI 日期选择器,那么是否有类似的东西可以使用?

最佳答案

我需要做同样的事情,因此使用 onSelectbeforeShowDay 事件编写了一些 JavaScript 来实现这一点。它维护自己的选定日期数组,因此不幸的是,它没有与显示当前日期等的文本框集成。我只是将它用作内联控件,然后我可以在数组中查询当前选定的日期。< br/> 我用过this code作为基础。

<script type="text/javascript">
// Maintain array of dates
var dates = new Array();

function addDate(date) {
    if (jQuery.inArray(date, dates) < 0) 
        dates.push(date);
}

function removeDate(index) {
    dates.splice(index, 1);
}

// Adds a date if we don't have it yet, else remove it
function addOrRemoveDate(date) {
    var index = jQuery.inArray(date, dates);
    if (index >= 0) 
        removeDate(index);
    else 
        addDate(date);
}

// Takes a 1-digit number and inserts a zero before it
function padNumber(number) {
    var ret = new String(number);
    if (ret.length == 1) 
        ret = "0" + ret;
    return ret;
}

jQuery(function () {
    jQuery("#datepicker").datepicker({
        onSelect: function (dateText, inst) {
            addOrRemoveDate(dateText);
        },
        beforeShowDay: function (date) {
            var year = date.getFullYear();
            // months and days are inserted into the array in the form, e.g "01/01/2009", but here the format is "1/1/2009"
            var month = padNumber(date.getMonth() + 1);
            var day = padNumber(date.getDate());
            // This depends on the datepicker's date format
            var dateString = month + "/" + day + "/" + year;

            var gotDate = jQuery.inArray(dateString, dates);
            if (gotDate >= 0) {
                // Enable date so it can be deselected. Set style to be highlighted
                return [true, "ui-state-highlight"];
            }
            // Dates not in the array are left enabled, but with no extra style
            return [true, ""];
        }
    });
});
</script>

关于jQuery UI Datepicker - 多个日期选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1452066/

相关文章:

jquery-ui - jQueryUI - 未捕获的异常 : cannot call methods

javascript - 在mvc中使用datepicker不起作用

iPhone UIDatePicker 设置MaximumDate

javascript - jquery UI Accordion 关闭问题

jQuery datepicker "dateFormat"和 "altFormat"之间的区别

javascript - 使用 php 脚本验证访问者的年龄并根据结果加载 iframe,无需刷新

jquery - 从 iframe 元素访问 jQuery 数据

javascript - 如何为 src 的最后三个字符是 certian 文件类型的图像设置 css?

javascript - ajax后显示成功消息

javascript - 制作 JQuery 扩展 $.fn.extension