开发者

Jquery Datepicker, at wits end

开发者 https://www.devze.com 2023-03-14 20:37 出处:网络
This 开发者_如何转开发code used to work but now it doesn\'t and it\'s only taking the first value in the array...

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);
    } 
    })


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)


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...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号