更新:我添加了a jsFiddle我的问题。当想法发生变化时,我也完全重写了这个问题。
我有一个页面,我使用knockout.js作为布局。我已经在我的应用程序中的另一个页面上成功地工作了,但我无法让这个工作。我的模型由父记录及其子记录组成。我的问题是当我尝试复制上一行时,用户可以更轻松地添加许多类似的记录。当我在“我的部门”,“项目”,“作业”和“注释”字段中键入值时,模型中的值为空白,因此当我复制这些字段时,它们保持空白。=。有任何想法吗?我猜我错过了一些愚蠢的东西。
function DayViewModel(day) {
var self = this;
self.Date = day.Date;
self.Time = day.Time;
self.Hours = ko.observable(day.Hours);
self.TimesheetCode = ko.observable(day.TimesheetCode);
self.Department = ko.observable(day.Department);
self.Project = ko.observable(day.Project);
self.Job = ko.observable(day.Job);
self.Comments = ko.observable(day.Comments);
self.JobIsRequired = ko.observable((day.JobIsRequired == undefined) ? false : day.JobIsRequired);
}
function RequestViewModel() {
var self = this;
self.DaysRequested = ko.observableArray([new DayViewModel({
Date: new Date().toString("MM/dd/yyyy"),
Time: "08:00 AM",
Hours: 1,
TimesheetCode: "",
Department: 0,
Project: 0,
Job: 0,
Comments: ""
})]);
self.timesheetCodes // gets data from an external data source
// Add another day copying the previous day.
self.addDay = function () {
var array = self.DaysRequested();
var previousDay = array[array.length - 1];
previousDay.Date = Date.parse(previousDay.Date).addDays(1).toString("MM/dd/yyyy");
var test = ko.utils.unwrapObservable(previousDay.Department);
var newDay = new DayViewModel({
Date: previousDay.Date.toString("MM/dd/yyyy"),
Time: previousDay.Time,
Hours: ko.utils.unwrapObservable(previousDay.Hours),
Department: ko.utils.unwrapObservable(previousDay.Department),
Project: ko.utils.unwrapObservable(previousDay.Project),
Job: ko.utils.unwrapObservable(previousDay.Job),
Comments: ko.utils.unwrapObservable(previousDay.Comments),
TimesheetCode: ko.utils.unwrapObservable(previousDay.TimesheetCode)
});
self.DaysRequested.push(newDay);
}
}
答案 0 :(得分:1)
当您需要值绑定时,您正在某些字段上使用文本绑定。例如,部门应该是
<input type="text" class="input-small department-entry" max="5" data-bind="value: Department, uniqueName: true" />
答案 1 :(得分:0)
除了你的可观察数组,你唯一可观察的是TimesheetCode。从ko documentation:
observableArray跟踪数组中的对象,而不是 这些对象的状态
我猜你已经将日期,时间,小时等绑定到页面上的输入框,并期望用户在这些输入框中输入的更新反映在底层的javascript对象中,但是不是,因此'Knockout值没有保存。'
如果是这种情况,那么我建议创建日期,时间,小时等作为可观察的。
如果您需要更详细的解决方案,请提供更详细的问题,包括相关标记。