2013-04-25 152 views
-2

更新的代碼JavaScript未定義?

CURRENT錯誤:遺漏的類型錯誤:無法讀取的不確定

我真的不知道這可能是問題財產「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'); 
} 
+2

變化當場解決它一鈕,然後交代碼,所以我們可以有一個幫助的希望。 – 2013-04-25 02:56:10

+0

第二個函數總是被稱爲_after_第一個函數嗎?請發佈您的代碼。 – nnnnnn 2013-04-25 02:59:32

+0

您如何期待任何人在沒有看到任何代碼的情況下幫助您? – Brad 2013-04-25 03:02:43

回答

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

我想我已經破譯了你所需要的東西。請看看jsFiddle Link,看看這是否適合你的問題。

下面的代碼:

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

});