我有一个状态的viewmodel,其中我有五个字段,其中五个字段是隐藏字段。我希望每当我触发ajax请求时都会更新。我的servlet响应一个值。我希望在ajax响应的基础上更新此值。这是我的viewmodel
var dummyData = [{ "nsc": "1", "name": "us", "bic": "united states", "identifier": "us" }, { "nsc": "2", "name": "europe", "bic": "europe", "identifier": "us" }, { "nsc": "3", "name": "aus", "bic": "aus", "identifier": "us" }];
function State(data) {
this.nsc = ko.observable(data.nsc || "");
this.nscto = ko.observable(data.nscto || "");
this.bic = ko.observable(data.bic || "");
this.name = ko.observable(data.name || "");
this.hiddenField = ko.observable(data.identifier || "");
}
function StateViewModel() {
var self = this;
self.States = ko.observableArray([]);
self.PopulateStates = ko.computed(function () {
ko.utils.arrayForEach(dummyData, function (item) {
self.States.push(new State(item));
});
});
self.nsc = ko.observable();
self.nscto = ko.observable();
self.bic = ko.observable();
self.hiddenField = ko.observable();
self.name = ko.observable();
}
这是我正在制作ajax请求的addState函数
self.addState = function (state) {
var stateData = ko.toJSON({ data: new State({ nsc: self.nsc(), nscto: self.nscto(), bic: self.bic(), name: self.name() }) });
$.ajax
({
data: "action=" + "addarea" + "&jsonData=" + stateData,
url: '/bin/stateUpdate/add',
dataType: 'json',
type: 'GET',
success: function (data) {
var identifier = data['identifier'];
alert(identifier); // checking servlet reponse
self.hiddenField(identifier);
self.States.push(new State({
nsc: self.nsc(),
nscto: self.nscto(),
bic: self.bic(),
name: self.name(),
hiddenField: self.hiddenField(identifier)
}));
},
error: function (exception) {
alert("fail");
}
});
/* $.post("/bin/stateUpdate/add", stateData, function(returnedData) {
bank.hiddenField(returnedData);
})*/
};
我想根据从servlet获取的值来更新hiddenField.addstate函数在viewmodel中。当我尝试使用Knockout方法$ .post进行ajax请求时,我怎么能设置这个值,它不起作用。有人可以帮忙吗?