-2

更新代码

当前错误:未捕获的类型错误:无法读取未定义的属性“id_cursa”

我真的不知道可能是哪个问题?

function locurilibere(data, callback) {
var URL = Path + 'rezervaribilete/locurilibere/' + data;
$.get(URL, function(obj) {
if (obj.raspuns === "nu") {
callback(true);
} else {
callback(false);
}
}, 'json');
}

function populateCurseDus(de_la, pana_la, data_plecarii) {
var data = de_la + "-" + pana_la + "-" + data_plecarii;
$.get(Path + 'rezervaribilete/listCurseDus/' + data, function(o) {
for (var i = 0; i < o.length; i++) {
var id_cursa = o[i].id_cursa;
var datalocuri = id_cursa + "-" + data_plecarii;
locurilibere(datalocuri, function(result){
if (result) {
$('#cursedus tbody').append('<tr style="background:red;"><td><input type="radio" name="id_cursadus" value="' + o[i].id_cursa + '" disabled></td><td>' + o[i].cod_cursa + '</td><td>' + o[i].de_la + '</td><td>' + o[i].pana_la + '</td><td>' + o[i].ora_plecare + '</td><td>' + o[i].ora_sosire + '</td><td>' + o[i].id_transportator + '</td><td>' + o[i].id_traseu + '</td></tr>');
} else {
$('#cursedus tbody').append('<tr><td><input type="radio" name="id_cursadus" value="' + o[i].id_cursa + '"></td><td>' + o[i].cod_cursa + '</td><td>' + o[i].de_la + '</td><td>' + o[i].pana_la + '</td><td>' + o[i].ora_plecare + '</td><td>' + o[i].ora_sosire + '</td><td>' + o[i].id_transportator + '</td><td>' + o[i].id_traseu + '</td></tr>');
}
});
}
}, 'json');
}
4

2 回答 2

1

由于 ajax 请求的异步性质,它不会按预期工作,您需要回调来修复它

function freeseats(data, callback) {
    var URL = Path + 'bookings/freeseats/' + data;
    $.get(URL, function(obj) {
        if (obj.raspuns === "nu") {
            // alert("no");
            callback(true);
        } else {
            // alert("yes");
            callback(false);
        }
    }, 'json');
}

// ********************************* second
// **************************************
function populateDepartures(from, to, departure) {
    var data = from + "-" + to + "-" + departure;
    $.get(Path + 'booking/listDepartures/' + data, function(o) {

        $.each(o, function(index, item) {
            var id_flight = item.id_flight;
            var dataseats = id_flight + "-" + departureDate;

            freeseats(dataseats, function(result) {
                if (result) {
                    alert("no more seats");
                    $('#cursedus tbody')
                    .append('<tr style="background:red;"><td><input type="radio"         name="id_cursadus" value="'
                            + item.id_cursa
                            + '" disabled></td><td>'
                            + item.cod_cursa
                            + '</td><td>'
                            + item.de_la
                            + '</td><td>'
                            + item.pana_la
                            + '</td><td>'
                            + item.ora_plecare
                            + '</td><td>'
                            + item.ora_sosire
                            + '</td><td>'
                            + item.id_transportator
                            + '</td><td>'
                            + item.id_traseu + '</td></tr>');
                } else {
                    alert("there are free seats");
                    $('#cursedus tbody')
                    .append('<tr><td><input type="radio" name="id_cursadus" value="'
                            + item.id_cursa
                            + '"></td><td>'
                            + item.cod_cursa
                            + '</td><td>'
                            + item.de_la
                            + '</td><td>'
                            + item.pana_la
                            + '</td><td>'
                            + item.ora_plecare
                            + '</td><td>'
                            + item.ora_sosire
                            + '</td><td>'
                            + item.id_transportator
                            + '</td><td>'
                            + item.id_traseu + '</td></tr>');
                }
            });
        });
    }, 'json');
}
于 2013-04-25T03:16:57.727 回答
0

我想我已经破译了你需要什么。请看一下这个jsFiddle 链接,看看这是否能解决您的问题。

这是代码:

var boolFlag = false;
var firstFunc = function (){
    if(boolFlag === false){
        boolFlag = true;
        return 'yes';    
     }else{
        boolFlag = false;
        return 'no';   
    }
};


var secondFunc = function () {
        return firstFunc();
};

$('#myButton').click(function (){
    if(secondFunc() == 'yes'){
        console.log('hello world, you said: YES'); 
    }else{
        console.log('hello universe, you said: NO'); 
    }

});
于 2013-04-25T03:15:05.293 回答