admin管理员组

文章数量:1419633

Anybody got any clue why this won't work with more than 1 date...

it only takes the first date in the array...

var unavailableDates = ["10-6-2011","13-6-2011"];

function unavailable(date) {
    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)];
    }
}

see full example below

.php

Thanks in advance

Lee

Anybody got any clue why this won't work with more than 1 date...

it only takes the first date in the array...

var unavailableDates = ["10-6-2011","13-6-2011"];

function unavailable(date) {
    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)];
    }
}

see full example below

http://offline.raileisure./lee.php

Thanks in advance

Lee

Share Improve this question edited Jun 3, 2011 at 13:51 Pointy 414k62 gold badges595 silver badges629 bronze badges asked Jun 3, 2011 at 13:45 LeeLee 1,2804 gold badges18 silver badges37 bronze badges 6
  • 3 You really should have posted the actual code that tries to use the array. Posting nothing more than a link to your site will not be helpful to Stackoverflow readers a year from now. – Pointy Commented Jun 3, 2011 at 13:47
  • Try providing some additional details on how exactly it fails. – Greg Commented Jun 3, 2011 at 13:47
  • @Greg the first date is disabled but the others in the array don't disable, its as if the code isn't reading anything after the first value in the array – Lee Commented Jun 3, 2011 at 13:49
  • Having your dates in an ambiguous format is almost always a bad idea. If you're going to use date strings, it's almost always best to stick with yyyy-mm-dd format. Also, whatever format you use, avoid single-digit days or months, as these can throw things out as well: use 06 rather than just 6 – Spudley Commented Jun 3, 2011 at 13:50
  • Define "this won't work" – Lightness Races in Orbit Commented Jun 3, 2011 at 13:51
 |  Show 1 more ment

4 Answers 4

Reset to default 8

It doesn't work because you're interpreting the return value of "$.inArray()" incorrectly. The function returns -1 when the search target cannot be found, and the index in the array when it can. Thus, when it returns 0, that means it did find what the code was looking for.

A cute trick — for those who like cute tricks — for checking the return value from functions like "$.inArray()" is to apply the "~" operator:

if (~$.inArray(needle, haystack)) {
  // found it
}
else {
  // did not find it
}

The "~" operator forms the bitwise plement (or "1's plement") of its argument. Because "~-1" is 0, and "~n" is non-zero for any other integer, it effectively converts the return value to a "truthy/falsy" value appropriately. Don't use it if you don't like cute tricks :-)

Also, that "dmy" variable used in a couple functions should be declared in each one with the var keyword.

jQuery.inArray returns the index of the item found, ie, when it matches the second value, it is returning 1, not the 0 you test for.

You should change your test to be >= 0 rather than == 0 when you do

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

Is it reading it in as mm-dd-yyyy?

If so - then 13-6-2011 would not be a valid date.

Edit Okay - looking at your page; clearly not since 10th June is not available as expected.

I deleted the answer but where dates are concerned I think this is a valuable thing to remember (e.g. on a US client presumably I'd be right?) so I undeleted it.

I'll get rid of it again though if the munity feels I should.

Try this: in pure javascript: I made ​​some modifications to make your code faster

function inArrayOrStr (o, v) {
    return ~o.indexOf(v);
}

unavailableDates = ["10-6-2011","13-6-2011"];

function unavailable(date) {
    var dmy = [date.getDate() ,(date.getMonth() + 1) , date.getFullYear()].join("-");
    if (inArrayOrStr(unavailableDates, dmy) {
        return [false, "", "Unavailable"];
    } else {
        var day = date.getDay();
        return [day > 7];
    }
}

本文标签: jqueryJavascript array not workingconfusedStack Overflow