0
我剛開始使用knockout.js
,並試圖顯示JSON數據時卡住了。Knockout applyBindings不適用綁定
我的HTML是
<p>Dealer Location: <input id="dealerlocation" data-bind="value: DealerLocation" /></p>
<p>Contact Report Date: <input id="crdate" data-bind="value: CRDate" /></p>
我的腳本塊
function viewAction() {
var self = this;
self.DealerLocation = ko.observable("");
self.CRDate = ko.observable("");
};
var viewActionModel = new viewAction();
function GetActionByID() {
$.ajax({
type: "POST",
url: "/ws/someservice.asmx/GetAction",
data: "{pacid: '" + $('input[id$=hidActionID]').val() + "'}",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (response) {
var action = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
$.each(action, function (index, axn) {
viewActionModel.DealerLocation = axn.DealerLocation;
viewActionModel.CRDate = axn.CRDate;
});
}
});
}
$(document).ready(function() {
GetActionByID();
//alert(viewActionModel.DealerLocation);
ko.applyBindings(viewActionModel);
});
注:如果我取消通知,然後applyBinding工作,否則它沒有。
這裏缺少什麼?
Ragards。
它現在完美的工作!但是當Alert()被放置在那裏時,爲什麼它在錯誤的賦值中工作? – user3104116
更新了我的答案。 – Anri
謝謝;這對我來說絕對是必須的。 – user3104116