我目前正在学习knockoutjs,我只是按照knockoutjs官方网站上关于列表和集合的教程,目前我拥有的是一个下拉列表,列出了我拥有的项目,然后旁边是一个显示文本的文本(价格),现在我想要发生的是显示的文本应该根据下拉菜单的选定项目而改变。
这是我的代码的 jsfiddle:http: //jsfiddle.net/UQskS/
另外,如果您发现我的代码有问题,除了我上面提到的,请给我建议,以获得最佳实践或更正。
// Class to represent a row in the seat reservations grid
function SeatReservation(name, initialMeal) {
//var self = this;
//self.name = ko.observable(name);
this.name = name;
this.meal = ko.observable(initialMeal);
this.formattedPrice = ko.computed(function () {
var price = this.meal().price;
return price ? "$" + price.toFixed(2) : "None";
}, this);
}
function ReservationsViewModel(name, meal) {
//var self = this;
// Non-editable catalog data - would come from the server
this.availableMeals = [
{ mealId: 1, mealName: "Standard (sandwich)", price: 47.55 },
{ mealId: 2, mealName: "Premium (lobster)", price: 34.95 },
{ mealId: 3, mealName: "Ultimate (whole zebra)", price: 290.123 }
];
//editable data
this.seats = ko.observableArray([
new SeatReservation("Randel", this.availableMeals[2]),
new SeatReservation("Knockout", this.availableMeals[1])
]);
//operations
this.addSeat = function () {
this.seats.push(new SeatReservation("", this.availableMeals[0]));
};
this.removeSeat = function (seat) {
this.seats.remove(seat);
;}
}
ko.applyBindings(new ReservationsViewModel());
先生/女士,您的回答会很有帮助。谢谢++