我很新的淘汰賽JS,我想我的手出來的例子,所以我有這個淘汰賽JS任意對象結合
<script>
var Country = function(name, population) {
this.countryName = name;
this.countryPopulation = population;
};
var viewModel = {
availableCountries : ko.observableArray([
new Country("UK", 65000000),
new Country("USA", 320000000),
new Country("Sweden", 29000000)
]),
selectedCountry : ko.observable() // Nothing selected by default
};
$(function(){ko.applyBindings(viewModel)});
</script>
,並在視圖
<p>Your country:
<select data-bind="options: availableCountries, optionsText: 'countryName', value: selectedCountry, optionsCaption: 'Choose...'"></select>
</p>
<div data-bind="visible: selectedCountry"> <!-- Appears when you select something -->
You have chosen a country with population
<span data-bind="text: selectedCountry() ? selectedCountry().countryPopulation : 'unknown'"></span>.
</div>
我的問題是我想下降到初始時有一個預先選定的值,所以我試過這個
selectedCountry : ko.observable(new Country("UK", 65000000))
但它不工作「選擇...」stil升顯示爲預選optionsText而不是「屋」那我就
selectedCountry : ko.observable(availableCountries[0])
,但我不斷收到此錯誤
「遺漏的類型錯誤:無法讀取屬性‘0’的未定義」
我做錯了什麼,我該如何解決它?
普雷斯托!!!!像魅力一樣工作! – Peace