以下是我的观点模型
var Patientp = function () {
this.id = ko.observable(idValue);
this.name = ko.observable(nameValue);
this.degree = ko.observable(degreeValue);
this.gender = ko.observable(genderValue);
//this.consultant= ko.observableArray(consultantArrValue);
this.username = ko.observable(usernameValue);
this.password = ko.observable(passwordValue);
this.email = ko.observable(emailValue);
this.mobile = ko.observable(mobileValue);
this.imgFile = ko.observable(imgFileValue);
this.imgSrc = ko.observable(imgSrcValue);
this.imagePath=ko.observable(imagePathValue);
this.userid=ko.observable(useridValue);
this.department=ko.observable(departmentValue);
//this.consultant= ko.observableArray(consultantArrValue);
//this.consultant= ko.observable(consultantValue);
}
我从ajax调用获得输入如下
$.ajax({
type: "GET",
url: projectUrl+"getDoctors",
dataType:"json",
jsonp: true,
async:false ,
success: function (data) {
//alert(data);
$.each(data.doctors, function(index, currPat) {
var doc = new Doctor(currPat.name,currPat.username);
doctors.push(doc);
if(currPat.userid=="${IDis}"){
console.log(currPat.degree);
nameValue = currPat.name;
usernameValue =currPat.username;
passwordValue ="" ;
emailValue = currPat.email;
mobileValue = currPat.mobile;
genderValue = currPat.gender;
departmentValue=currPat.department;
degreeValue=currPat.degree;
imgSrcValue=currPat.imagePath;
}
});
}
});
现在所有输入字段都有旧值。现在我想更改一些输入字段,当我按下保存按钮后,我想要JSON中的值,以便我可以传入ajax格式。所以我使用ko.toJSON之类的以下
$('#saveButton').click(function(){
alert('savebutton');
var testjson=ko.toJSON(new Patientp());
console.log(testjson);
});
请参见截图,移动设备值为1234567890
现在我将移动设备移至666并按下保存按钮,看到移动设备(控制台中的JSON)仍未更改
但在这里我只得到旧的值。可以告诉我如何获取新值(我在输入字段中键入的内容)
答案 0 :(得分:0)
我在new Patientp()
进行了两次ko.applyBindings(new Patientp())
次,而另一次var testjson=ko.toJSON(new Patientp());
现在我做的时候
var patp=new Patientp();
之后ko.applyBindings(patp)
和var testjson=ko.toJSON(patp);
然后问题就解决了