我无法获取绑定的数据绑定的值。当我尝试读取该值时,它始终是未定义的。
我在这里设置了一个小提琴:
http://jsfiddle.net/tonymaloney1971/2qjhb5pw/5/
我认为问题可能出在我设置敲除绑定的方式上:
$(document).ready(function () {
var data = [{ PostCodeStart: "", PostCodeEnd: "", Mileage: "", Notes: "" }];
add: function () {
//this part is not working this.PostCodeStart() === ""
alert("How do I get the value of PostCodeStart");
if (this.PostCodeStart() === "" || this.PostCodeEnd() === "" || this.Mileage() === "") {
alert("empty field");
}
else
this.journeyList.push({ PostCodeStart: this.PostCodeStart(), PostCodeEnd: this.PostCodeEnd(), Mileage: this.Mileage(), Notes: this.Notes() });
},
另外,在我的小提琴中,您会注意到
谢谢
我做了一个修改过的小提琴,将数据添加到您的添加和删除函数中。通常,您将不会使用this
淘汰赛。附带说明一下,道格拉斯·克罗克福德(Douglas Crockford)提出了一个公平的理由,那就是this
在谈论无类OOP时不要使用。
以下是相关的HTML:
<button class="btn-success img-rounded" data-bind="click:$root.add">
<span class="glyphicon glyphicon-plus-sign" style="text-align:right"></span>
</button>
<button class="btn-danger img-rounded" data-bind="click:$root.remove">
<span class="glyphicon glyphicon-remove"></span>
</button>
和viewModel:
var viewModel = {
journeyList: ko.observableArray(data),
add: function (data) {
if (data.PostCodeStart === "" || data.PostCodeEnd === "" || data.Mileage === "") {
alert("empty field");
}
else {
viewModel.journeyList.push({ PostCodeStart: data.PostCodeStart, PostCodeEnd: data.PostCodeEnd, Mileage: data.Mileage, Notes: data.Notes });
}
},
remove: function (data) {
viewModel.journeyList.remove(data);
}
};
更新:在列表中添加样式以消除项目符号。
<ul data-bind="foreach: journeyList" style="list-style-type:none">
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句