我有一些数据绑定到视图。已使用 cleanNode 取消绑定数据并清除视图。这是相同的jsfiddle:http: //jsfiddle.net/FENuV/26/
相同的代码如下: 查看:
<div id="QdetailsDiv">
<div>
<div>
<div><span>Sold to Account</span></div>
<div>
<input type="text" id="txtSoldToAccount" data-bind="value:SoldToAccount"/>
</div>
</div>
<div>
<div><span>Job Name</span></div>
<div>
<input type="text" id="txtQDJobName" data-bind="value:jobName"/></div>
</div>
</div>
<input type="button" value="unbind" id="unbindbutton"></input>
</div>
查看型号:
QuoteDetails = function () {
var self = this;
self.SoldToAccount = ko.observable();
self.jobName = ko.observable();
};
function SimpleSearchResults() {
var self = this;
self.index = ko.observable();
self.quoteDetailsObj = ko.observable(new QuoteDetails());
self.productDetailsArrObj = ko.observableArray([]);
self.BindDataToQuotesDetailVM = function (SimpleSearchResultsOb) {
var self= this;
//SimpleSearchResultsOb['SoldToAccount']('SoldToAccount test');
SimpleSearchResultsOb.SoldToAccount = "SoldToAccount test";
SimpleSearchResultsOb.jobName = ko.observable("jobName test");
ko.applyBindings(SimpleSearchResultsOb, document.getElementById("QdetailsDiv"));
};
};
function QuoteSimpleSearchVM() {
var self = this;
self.simpleSearchResultsArray = ko.observableArray([]);
var SimpleSearchResultsObj= new SimpleSearchResults();
SimpleSearchResultsObj.index=ko.observable("1");
self.simpleSearchResultsArray.push(SimpleSearchResultsObj);
var SimpleSearchResultsObj2= new SimpleSearchResults();
SimpleSearchResultsObj2.index=ko.observable("2");
self.simpleSearchResultsArray.push(SimpleSearchResultsObj2);
};
var obj= new QuoteSimpleSearchVM();
$(document).ready(function () {
$("#unbindbutton").click(function(){
var element = $('#QdetailsDiv')[0];
ko.cleanNode(element);
});
ko.utils.arrayForEach(obj.simpleSearchResultsArray(), function (SimpleSearchResultsObj) {
if (SimpleSearchResultsObj.index() === "2") {
SimpleSearchResultsObj.BindDataToQuotesDetailVM(this);
}
});
});
在 cleanNode 上,数据不会从视图中清除。任何人都可以帮助我解决问题。
提前致谢。