以下のコードで私を助けてください。
オブジェクト「airported」が空港のリストを返すようにしたいのですが、現在、各ループが終了した後にデータを返すことができません.list.htmlで「li」を見つける反復です(ajax経由で受信)
Airported オブジェクトは、Airport クラスのインスタンス化を使用しています。アプリケーションの他の場所に複雑なコードは必要ありません。「airport.list()」を呼び出して、空港のリストを取得したいだけです。
list.html は参考用です。勉強する必要はありません。また、私はそれを使用する必要があります。きれいなJSONなどはありません。
Javascript
function Airport() {
this.list = function() {
var airportList = []
var promise = this.data();
promise.success(function(data){
var list = data;
var listSize = $(data).find('li').size();
$.each($(list).find("li"), function(key,val) {
airportList.push(val);
if(listSize == key+1) {
console.log(airportList);
return false;
}
});
})
return airportList;
};
this.data = function() {
return $.ajax({
url: "/list.html"
})
};
}
var airported = new Airport();
console.log('airported', airported.list());
list.html
<html>
<head></head>
<body>
<div id="listautocomp" style="background:white">
<ul id="ulSuggest">
<li style="padding:0px;">
<div id="WAS|0|0***<b>WAS</b> - <b>Was</b>hington All Airports, District of Columbia, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:hidden;;"></span><b>WAS</b>
- <b>Was</b>hington All Airports, District of Columbia, United States
</div>
</li>
<li style="padding:0px;">
<div id="SEA|1|0***SEA - Seattle/Tacoma, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>SEA
- Seattle/Tacoma, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="SEA|0|0***SEA - Seattle All Airports, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:hidden;;"></span>SEA
- Seattle All Airports, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="IAD|1|0***IAD - <b>Was</b>hington Dulles, District of Columbia, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>IAD
- <b>Was</b>hington Dulles, District of Columbia, United States
</div>
</li>
<li style="padding:0px;">
<div id="DCA|1|0***DCA - <b>Was</b>hington National, District of Columbia, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>DCA
- <b>Was</b>hington National, District of Columbia, United States
</div>
</li>
<li style="padding:0px;">
<div id="GEG|1|0***GEG - Spokane, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>GEG
- Spokane, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="GEG|0|0***GEG - Spokane All Airports, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:hidden;;"></span>GEG
- Spokane All Airports, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="PSC|1|0***PSC - Pasco, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>PSC
- Pasco, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="BLI|1|0***BLI - Bellingham, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>BLI
- Bellingham, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="YKM|1|0***YKM - Yakima Air Terminal, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>YKM
- Yakima Air Terminal, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="YKM|0|0***YKM - Yakima All Airports, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:hidden;;"></span>YKM
- Yakima All Airports, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="LKE|0|0***LKE - Seattle LKE Union, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:hidden;;"></span>LKE
- Seattle LKE Union, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="EAT|1|0***EAT - Wenatchee, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>EAT
- Wenatchee, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="PUW|1|0***PUW - Pullman, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>PUW
- Pullman, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="ALW|1|0***ALW - Walla Walla, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>ALW
- Walla Walla, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="MWH|1|0***MWH - Moses Lake, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>MWH
- Moses Lake, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="MWH|0|0***MWH - Moses Lake All Airports, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:hidden;;"></span>MWH
- Moses Lake All Airports, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="CLM|1|0***CLM - Port Angeles, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>CLM
- Port Angeles, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="OLM|1|0***OLM - Olympia, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>OLM
- Olympia, <b>Was</b>hington, United States
</div>
</li>
<li style="padding:0px;">
<div id="PAE|1|0***PAE - Everett, <b>Was</b>hington, United States"
onclick="SetLocationStrings(this.id)" onmouseover="SuggestMouseOver(this)"><span class="airporticon"
style="visibility:visible;;"></span>PAE
- Everett, <b>Was</b>hington, United States
</div>
</li>
</ul>
</div>
</body>
</html>