Jquery Datepicker,束手无策

发布于 2024-11-16 18:49:08 字数 1350 浏览 2 评论 0原文

这段代码过去可以工作,但现在不行了,它只取数组中的第一个值...

var unavailableDates 是一个停止在日期选择器上显示日期的数组。

有什么想法吗?

由于某种原因,它没有循环遍历数组中的所有日期!?!?

var unavailableDates = ["4-7-2011","5-7-2011"];

function unavailable(date) {
    var dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
    if ($.inArray(dmy, unavailableDates) == 0) {
        return [false, "", "Unavailable"];
    } else {
        var day = date.getDay();
        return [(day != 0 && day != 2 && day != 3 && day != 4 && day != 6)];
    }
}

$(function(){

    $('#smh').datepicker({
        showOn: "both",
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true,
        beforeShowDay: unavailable,
        minDate: -0,
        dateFormat: "dd/mm/yy",
        onSelect: function(e) {
        e = e.split('/')[1] + '/' + e.split('/')[0] + '/' + e.split('/')[2];
        var date = new Date(e);
        var day = date.getDay(); // 0 = sunday etc...        
        if (day === 1) {
            $("#check2").hide();
            $("#text").hide();
            $("#check1").show();

        } else if (day === 5) {
            $("#check1").hide();
            $("#text").hide();
            $("#check2").show();

        } 
        $("#bdate").html(this.value);
    } 
    })

This code used to work but now it doesn't and it's only taking the first value in the array...

var unavailableDates is an array that stops dates showing on the datepicker..

any ideas??

It's not cycling through all dates in the array for some reason!?!?

var unavailableDates = ["4-7-2011","5-7-2011"];

function unavailable(date) {
    var dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
    if ($.inArray(dmy, unavailableDates) == 0) {
        return [false, "", "Unavailable"];
    } else {
        var day = date.getDay();
        return [(day != 0 && day != 2 && day != 3 && day != 4 && day != 6)];
    }
}

$(function(){

    $('#smh').datepicker({
        showOn: "both",
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true,
        beforeShowDay: unavailable,
        minDate: -0,
        dateFormat: "dd/mm/yy",
        onSelect: function(e) {
        e = e.split('/')[1] + '/' + e.split('/')[0] + '/' + e.split('/')[2];
        var date = new Date(e);
        var day = date.getDay(); // 0 = sunday etc...        
        if (day === 1) {
            $("#check2").hide();
            $("#text").hide();
            $("#check1").show();

        } else if (day === 5) {
            $("#check1").hide();
            $("#text").hide();
            $("#check2").show();

        } 
        $("#bdate").html(this.value);
    } 
    })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆那么伤 2024-11-23 18:49:08

在 jQuery 中,当在 array 中找不到 elem 时,$.inArray(elem, array) 方法返回 -1,而不是 0。

所以我认为你应该使用:

if ($.inArray(dmy
, unavailableDates) == -1)

而不是:

if ($.inArray(dmy, unavailableDates) == 0)

In jQuery the $.inArray(elem, array) method returns -1, and not 0, when elem is not found in array.

So I think you should use:

if ($.inArray(dmy
, unavailableDates) == -1)

instead of:

if ($.inArray(dmy, unavailableDates) == 0)

墨洒年华 2024-11-23 18:49:08

发生的情况是 $.inArray 返回找到的项目的索引(如果找到,则为 -1),

因此您需要检查索引是否不为 -1

...$.inArray(dmy, unavailableDates) != -1...

What happens is $.inArray returns the index of the found item(if it finds it, else -1)

so youll need to check if the index is not -1

...$.inArray(dmy, unavailableDates) != -1...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文