我是新來的淘汰賽,並有一個問題讓從下拉列表中選擇的值。我試圖改變價值「selectedCity
」和我得到[Object object]
。謝謝!入門選擇從下拉列表中值Knockout.js
我的HTML
<select data-bind="options: Cities, optionsText: 'CityNameRu', value: selectCity">
</select>
<span data-bind="text: selectedCity"></span>
淘汰賽
function CityModel(data) {
var self = this;
this.CityId = ko.observable(data.CityId);
this.CityNameRu = ko.observable(data.CityNameRu);
this.CityName = ko.observable(data.CityName);
}
function ViewModel() {
var self = this;
self.Cities = ko.observableArray([]);
self.selectedCity = ko.observable();
self.selectCity = function (city) {
self.selectedCity(city.CityNameRu);
};
self.GetCities = function() {
$.ajax({
type: "GET",
url: '/FetchCities',
dataType: "json",
success: function (data) {
self.SuccessfullyRetrievedModelsFromAjax(data);
},
error: function (err) {
alert(err.status + " : " + err.statusText);
}
});
};
this.SuccessfullyRetrievedModelsFromAjax = function (models) {
ko.utils.arrayForEach(models, function (model) {
self.Cities.push(new CityModel(model));
});
};
JSON響應:
[{"CityId":1,"CityName":"philadelphia","CityNameRu":"Филадельфия"},{"CityId":2,"CityName":"new-york","CityNameRu":"Нью Йорк"}
「我試圖改變價值‘selectedCity’和我得到的翻譯:」你在哪裏在控制檯中看到這一點?哪行代碼打印此消息? – onetwo12
這裏: –