在我的HTML页面中有3个输入。使用用户输入,我希望使用KnockoutJS更新我的页面元素。这是我写的脚本:在KnockoutJS中将数据推送到数组的问题
$(document).ready(function(){
function Task(data){
this.goal=ko.observable(data.goal);
this.type=ko.observable(data.type);
this.date=ko.observable(data.date);
console.log("Data"+ " " + data.goal);
}
var myViewModel=function(tasks){
var self=this;
self.tasks=ko.observableArray([{goal:"abc", type:"Intermediate", date:"12/13/1122"}]);
self.newGoalText=ko.observable("");
self.newTypeText=ko.observable("");
self.newDateText=ko.observable("");
self.addTask=function(){
self.tasks.push(new Task({goal:this.newGoalText(),type:this.newTypeText(), date:this.newDateText()}));
console.log(tasks);
self.newGoalText("");
self.newTypeText("");
self.newDateText("");
}//addTask function
}//viewModel
ko.applyBindings(new myViewModel())
});
console.log告诉我,值是从用户获得的预期。但是,任务数组上的“推”方法似乎根本没有效果。请指导我。
问题可能在于你的html,所以它会有帮助,如果你发布它。我测试了你的代码(没有改变任何东西),它和我提供的标记一起工作。见https://jsfiddle.net/ffojupuf/ – Adrian
@Adrian,谢谢你的帮助。我的HTML代码和你的类似,我没有发现很多不同的wrt绑定。不知道出了什么问题,但!我使用“值”而不是“textInput”。这是唯一的区别。 –