我只是在消磨时间,用一些 Knockout.js 弄脏我的手。我以前从未靠近过它,所以我想我会看看它的全部内容。在官方网站 - http://learn.knockoutjs.com 上,我修改了第 4/5 步的代码,以便在单击按钮时将 lastName 值转换为大写,而不是将 firstName 和 lastName 组合起来进入名为 fullName 的变量,并且视图更新为以大写形式显示 fullName。知道我在这里做错了什么吗?我只是试图聚合/连接 firstName 和 lastName 的值,并将它们存储在名为 currentVal 的 var 中,该变量在视图中分配给值 fullName。当我点击按钮时,什么都没有发生,呵呵这是我的代码(从教程的第 4/5 步修改,看看你能不能告诉我我在这里可能做错了什么。// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UIfunction AppViewModel() { this.firstName = ko.observable("Bert"); this.lastName = ko.observable("Bertington"); this.fullName = ko.computed(function() { return this.firstName() + " " + this.lastName(); }, this); this.capitalizeLastName = function() { this.fullName = this.firstName() + " " + this.lastName(); //with or without this line, it doesn't update fullName to uppercase :-| var currentVal = this.fullName(); this.fullName(currentVal.toUpperCase()); };}// Activates knockout.jsko.applyBindings(new AppViewModel());<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script><p>First name: <strong data-bind="text: firstName"></strong></p><p>Last name: <strong data-bind="text: lastName"></strong></p><p>First name: <input data-bind="value: firstName" /></p><p>Last name: <input data-bind="value: lastName" /></p><p>Full Name: <input data-bind="value: fullName" /></p><p>Full Name: <strong data-bind="text: fullName"></strong></p><button data-bind="click: capitalizeLastName"> Go caps</button>
添加回答
举报
0/150
提交
取消