weixin_33725126 2017-10-31 22:55 采纳率: 0%
浏览 79

jQuery数组不起作用

I have this simple array, but I cannot access the elements inside it. I have this ajax code in which I'm getting booked dates and storing them in an array. Then I'm just check if the selected date is booked or not by simple inArray but its not working. I have also tried getting the direct value but it gives me undefined.

AJAX

window.carRentalBookedOutDays = [];

var dataObj = {
    'action':'car_rental_booked_dates_request',
    'car_rental_id' : carRentalId,
    'month' : month,
    'year' : year,
    'nonce' : BYTAjax.nonce
};

$.ajax({
    url: BYTAjax.ajaxurl,
    data: dataObj,
    success:function(json) {
        // This outputs the result of the ajax request
        var bookedDates = JSON.parse(json);

        var i = 0;
        for (i = 0; i < bookedDates.length; ++i) {
            window.carRentalBookedOutDays.push(bookedDates[i].booking_date);
        }

        if (typeof(callDelegate) !== 'undefined') {
            callDelegate();
        }
    },
    error: function(errorThrown) {

    }
});

Checking Condition:

var datesArray = window.carRentalBookedOutDays;
var dateToCheck = "2017-11-03";

console.log(datesArray);

// returns undefined
console.log(datesArray[0]);

if ($.inArray(dateToCheck, datesArray) > -1) {
    dateTest = false;
    break;
}
if ($.inArray(dateToCheck2, datesArray) > -1) {
    dateTest = false;
    break;
}

Output: http://take.ms/HRK76

  • 写回答

1条回答 默认 最新

  • weixin_33682790 2017-10-31 23:49
    关注

    Something strange was happening, if I added a delay in the checking script it worked. But adding delay was not the right option.

    I ended up verifying the dates in the ajax success method itself, it was working fine in there.

    I think this issue is related to this

    评论

报告相同问题?