Knockout.JS,模板都是通过表单不添加数据的
Knockout.JS , templates are through form no data added
我是Knockout的新手,在向视图中添加模板时遇到了问题。到目前为止,我正在使用Jquery和Knockout来制作一个小程序,在这个程序中,您可以填写表单,并向对象数组添加一行。我使用了以下代码,但我不明白为什么新制作的模板没有复制我传递给他们的信息。当我使用标题中的表格(它弹出)时,我填写信息并按添加,会添加新的模板,但其中没有信息。你能帮我吗?
<body>
<div class="header" data-role="header">
<a href="#add_popup" data-rel="popup" data-transition="slide" data-role="button" class="ui-btn-right" data-icon="plus">Add a friend...</a>
</div>
<div class="page">
<div class="profiles">
<div data-bind="template: { name: 'person-template', foreach: friend }">
</div>
</div>
</div>
<div data-role="popup" id="add_popup" class="function_popup" data-overlay-theme="b" data-position-to="window">
<h3>What is your friends information?</h3>
<form data-bind="submit: addFriend">
Add task:
<input data-bind="value: newfirstName" name="firstname" placeholder="First Name..." / required>
<input data-bind="value: newlastName" name="lastname" placeholder="Last Name..." / required>
<input data-bind="value: newJob" placeholder="Job Title" / required>
<input data-bind="value: newUrl" type="url" placeholder="Facebook Link" />
<input data-bind="value: newTelephone" type="tel" placeholder="Telephone Number" />
<input data-bind="value: newCity" placeholder="City" / required>
<input data-bind="value: newCountry" placeholder="Country" / required>
<button type="submit">Add</button>
</form>
</div>
<script type="text/html" id="person-template">
<div class="friend">
<h3 data-bind="text: firstName + ' ' + lastName"></h3>
<img class="friend_pic" src="profile.jpg" /></a>
<p><span data-bind="text: job"></span></p>
<p><span data-bind="text: city +','+ ' ' + country "></span></p>
</div>
</script>
<script type="text/javascript">
function Friend(data) {
this.newfirstName = ko.observable(data.newfirstName);
this.newlastName = ko.observable(data.newlastName);
this.newJob = ko.observable(data.newJob);
this.newUrl = ko.observable(data.newUrl);
this.newTelephone = ko.observable(data.newTelephone);
this.newCity = ko.observable(data.newCity);
this.newCountry = ko.observable(data.newCountry);
}
function ViewModel() {
this.friend = ko.observableArray ([
{ firstName: 'Franklin', lastName: 'Johnson', job: 'Marketeer', url:'http://www.facebook.com', telephone: '+31654673995', city:'London', country:'UK'},
{ firstName: 'John', lastName: 'Richards', job: 'Banker', url:'http://www.facebook.com', telephone: '+4551891791', city:'Atlanta', country:'USA'}]);
this.newfirstName = ko.observable();
this.newlastName = ko.observable();
this.newJob = ko.observable();
this.newUrl = ko.observable();
this.newTelephone = ko.observable();
this.newCity = ko.observable();
this.newCountry = ko.observable();
};
this.addFriend = function() {
this.friend.push(new Friend({ firstName: this.newfirstName(),
lastName: this.newlastName(),
job: this.newJob(),
url:this.newUrl(),
telephone: this.newTelephone(),
city:this.newCity(),
country:this.newCountry()}));
this.newfirstName("");
this.newlastName = ("");
this.newJob = ("");
this.newUrl = ("");
this.newTelephone = ("");
this.newCity = ("");
this.newCountry = ("");
};
ko.applyBindings(new ViewModel());
</script>
</body>
</html>
this
会引起问题。带有addFriend的代码移回了视图模型中,这个/self/var修复了:
<script type="text/javascript">
function Friend(data) {
var self = this;
self.firstName = ko.observable(data.newfirstName);
self.lastName = ko.observable(data.newlastName);
self.job = ko.observable(data.newJob);
self.url = ko.observable(data.newUrl);
self.telephone = ko.observable(data.newTelephone);
self.city = ko.observable(data.newCity);
self.country = ko.observable(data.newCountry);
}
function ViewModel() {
var friend = ko.observableArray([{
firstName : 'Franklin',
lastName : 'Johnson',
job : 'Marketeer',
url : 'http://www.facebook.com',
telephone : '+31654673995',
city : 'London',
country : 'UK'
}, {
firstName : 'John',
lastName : 'Richards',
job : 'Banker',
url : 'http://www.facebook.com',
telephone : '+4551891791',
city : 'Atlanta',
country : 'USA'
}
]);
var newfirstName = ko.observable();
var newlastName = ko.observable();
var newJob = ko.observable();
var newUrl = ko.observable();
var newTelephone = ko.observable();
var newCity = ko.observable();
var newCountry = ko.observable();
var addFriend = function () {
friend.push(new Friend({
firstName : newfirstName(),
lastName : newlastName(),
job : newJob(),
url : newUrl(),
telephone : newTelephone(),
city : newCity(),
country : newCountry()
}));
newfirstName("");
newlastName = ("");
newJob = ("");
newUrl = ("");
newTelephone = ("");
newCity = ("");
newCountry = ("");
};
return {
friend: friend,
newfirstName: newfirstName,
newlastName: newlastName,
newJob: newJob,
newUrl: newUrl,
newTelephone: newTelephone,
newCity: newCity,
newCountry: newCountry,
addFriend: addFriend
};
};
ko.applyBindings(new ViewModel());
</script>
相关文章:
- jQuery表单添加不适用于下拉列表
- 如何在javascript转换编码中将一个动态值从一个表单添加到另一个页面
- 根据下拉选择向表单添加和删除输入标记
- 向表单添加滚动功能
- 如何根据用户输入的数字动态地向表单添加文本字段
- 在firefox中,在向表单添加onsubmit属性后,不会提交表单
- 在rails中选择向表单添加文本字段的最佳方式
- 使用 HTML 按钮向输入和提交表单添加文本
- 如何在 PHP 中向此联系表单添加验证
- 使用 Ajax 和 PHP 在外部 XML 文件中从 HTML 表单添加新数据
- 按钮从外部表单添加选定=“选定”
- 使用 Javascript 将详细的输入表单添加到测验结果中
- jQuery 移动表单:添加的字段没有样式
- 向动态表单添加验证
- 在Meteor TODO应用程序中向表单添加另一个字段
- 我可以通过Javascript向表单添加参数吗
- 向表单添加多个字段
- 如何在Javascript中向表单添加隐藏的ID列表
- 向PHP表单添加更多字段(用户可以添加多个地址)
- 向表单添加动态字段