- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从表单字段获取输入字段的js代码如下
var DocSchedule=function(id,day,fromtime,totime,hospital,hospitalId)
{
this.id=ko.observable(id);
this.day=ko.observable(day);
this.fromtime=ko.observable(fromtime);
this.totime=ko.observable(totime);
this.hospital=ko.observable(hospital);
this.hospitalId=ko.observable(hospitalId);
}
var iter=0;
var genderOptions = ['Male', 'Female'];
var degreeOptions=['BDS','DA(Anaesthesia)','MBBS','MBBS,MD','MBBS,MD(Med)PMCH','MBBS,MD,FNB','MBBS,MS(ENT)','MD,DM,FISC','MD,MS,PhD,DSc','MDS(Oral Surgery)','MS(OPHTH),FICS','MS,DNB,MRCS(Edin),MCh(GASTRO)']
var departmentOptions=['Anesthesiology','Bio-Chemistry','Cardiac Rehab Yoga','Cardio Thoracic Surgery','Cardiology','Chest Physician','Cosmetic Plastic and Hand Surgery','Critical Care','Dental&Facio maxillary Surgery','Dermatology','Diabetology','Dietary Services','Emergency Medicine','Endocrinology','Endoscopic,Head & Neck Surgery','Endoscopy','Gastroenterology','Gastrointestinal Medicine','General Medicine','General Surgery','Geriatrics','Gynecology','Hematology','Internal Medicine','Interventional Radiology','Laboratory Medicine','Laparoscopic Surgery','Medical Oncology','Micro-Biology','Nephrology','Neuro-Surgery','Neurology','Nuclear Medicine','Nuclear Medicinee','Obstetrics and Gynecology','Ophthalmology','Orthopedics & Traumatology','Otorhinolaryngology','Pathology','Pediatric Cardiology','Pediatric Surgery','Pediatrics','Physician','Physiotherapy','Psychiatry','Pulmonology','Radio-Diagnosis','Radiology','Rheumatology','Surgical Gastro-Enterology','Surgical Oncology','Urology','Vascular Surgery']
var Doctor = function(nameParam, usernameParam){
this.name = nameParam;
this.username = usernameParam;
};
var doctors = [];
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);
this.addSlot=function(doctor)
{
var docSchedule=new DocSchedule();
iter=iter+1;
docSchedule.id(iter);
doctor.schedules.push(docSchedule);
}
}
//idValue = 'hi';
//useridValue = 'hi';
//nameValue = 'hi';
addressValue = 'adress';
//genderValue = 'hi';
//mobileValue = 'hi';
//these fields are not available
//usernameValue = 'hi';
//passwordValue = 'hi';
//emailValue = 'hi';
imgFileValue = 'imagefileValue';
//imgSrcValue = 'ui_resources/img/profile_pic.png';
imagePathValue = 'imagePathValue';
//consultantArrValue = null;//'${currentpatient.user.name}';
//consultantValue="d1";
//var doc={};
var projectUrl=$('#projectUrl').val();
//alert(projectUrl);
/* var test=$.ajax({
type: "GET",
url: projectUrl+"getDoctors",
dataType:"json",
jsonp: true,
async:false
}).responseText;
console.log(test); */
$.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;
useridValue=currPat.userid;
idValue=currPat.id;
}
});
}
});
/* var jsonobject=ko.utils.parseJson(test);
$.each(jsonobject.patients,function(i,item){
var patient1 = new Patient(item.id, item.name, item.address, item.gender,item.consultant,item.username,"",item.email,item.mobile,"","",item.userid,item.imagePath);
self.patients.push(patient1);
}); */
var patp=new Patientp();
ko.applyBindings(patp);
$('#saveButton').click(function(){
alert('savebutton');
var testjson=ko.toJSON(patp);
console.log(testjson);
var formdata=new FormData();
formdata.append("doctormetada",testjson);
console.log(projectUrl+"updateDoctor");
$.ajax({
url: projectUrl+"updateDoctor",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
formdata = new FormData();
//self.doctor(new Doctor());
}
});
});
查看此后answer
将我的代码更改为
var DocSchedule=function(id,day,fromtime,totime,hospital,hospitalId)
{
this.id=ko.observable(id);
this.day=ko.observable(day);
this.fromtime=ko.observable(fromtime);
this.totime=ko.observable(totime);
this.hospital=ko.observable(hospital);
this.hospitalId=ko.observable(hospitalId);
}
var iter=0;
var genderOptions = ['Male', 'Female'];
var degreeOptions=['BDS','DA(Anaesthesia)','MBBS','MBBS,MD','MBBS,MD(Med)PMCH','MBBS,MD,FNB','MBBS,MS(ENT)','MD,DM,FISC','MD,MS,PhD,DSc','MDS(Oral Surgery)','MS(OPHTH),FICS','MS,DNB,MRCS(Edin),MCh(GASTRO)']
var departmentOptions=['Anesthesiology','Bio-Chemistry','Cardiac Rehab Yoga','Cardio Thoracic Surgery','Cardiology','Chest Physician','Cosmetic Plastic and Hand Surgery','Critical Care','Dental&Facio maxillary Surgery','Dermatology','Diabetology','Dietary Services','Emergency Medicine','Endocrinology','Endoscopic,Head & Neck Surgery','Endoscopy','Gastroenterology','Gastrointestinal Medicine','General Medicine','General Surgery','Geriatrics','Gynecology','Hematology','Internal Medicine','Interventional Radiology','Laboratory Medicine','Laparoscopic Surgery','Medical Oncology','Micro-Biology','Nephrology','Neuro-Surgery','Neurology','Nuclear Medicine','Nuclear Medicinee','Obstetrics and Gynecology','Ophthalmology','Orthopedics & Traumatology','Otorhinolaryngology','Pathology','Pediatric Cardiology','Pediatric Surgery','Pediatrics','Physician','Physiotherapy','Psychiatry','Pulmonology','Radio-Diagnosis','Radiology','Rheumatology','Surgical Gastro-Enterology','Surgical Oncology','Urology','Vascular Surgery']
var Doctor = function(nameParam, usernameParam){
this.name = nameParam;
this.username = usernameParam;
};
var doctors = [];
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);
this.addSlot=function(doctor)
{
mm = doctor
console.log("this", doctor);
var docSchedule = new DocSchedule();
iter++;
docSchedule.id(iter);
this.schedulers().push(docSchedule);
}
}
//idValue = 'hi';
//useridValue = 'hi';
//nameValue = 'hi';
addressValue = 'adress';
//genderValue = 'hi';
//mobileValue = 'hi';
//these fields are not available
//usernameValue = 'hi';
//passwordValue = 'hi';
//emailValue = 'hi';
imgFileValue = 'imagefileValue';
//imgSrcValue = 'ui_resources/img/profile_pic.png';
imagePathValue = 'imagePathValue';
//consultantArrValue = null;//'${currentpatient.user.name}';
//consultantValue="d1";
//var doc={};
var projectUrl=$('#projectUrl').val();
//alert(projectUrl);
/* var test=$.ajax({
type: "GET",
url: projectUrl+"getDoctors",
dataType:"json",
jsonp: true,
async:false
}).responseText;
console.log(test); */
$.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;
useridValue=currPat.userid;
idValue=currPat.id;
}
});
}
});
/* var jsonobject=ko.utils.parseJson(test);
$.each(jsonobject.patients,function(i,item){
var patient1 = new Patient(item.id, item.name, item.address, item.gender,item.consultant,item.username,"",item.email,item.mobile,"","",item.userid,item.imagePath);
self.patients.push(patient1);
}); */
//var patp=new Patientp();
//ko.applyBindings(patp);
function vm() {
var self = this;
self.person = new Patientp();
self.schedule = new DocSchedule();
self.schedules = ko.observableArray([new DocSchedule(iter)]);
self.addSlot = function () {
console.log(self.schedules.length);
iter++;
var docSchedule = new DocSchedule(iter);
self.schedules.push(docSchedule);
};
self.removeSlot = function () {
console.log('removed');
self.schedules.remove(this);
}
};
var viewModel = new vm();
ko.applyBindings(viewModel, document.getElementById('addDoctorSchedules'));
$('#saveButton').click(function(){
alert('savebutton');
var testjson=ko.toJSON(self.person);
console.log(testjson);
var formdata=new FormData();
formdata.append("doctormetada",testjson);
console.log(projectUrl+"updateDoctor");
$.ajax({
url: projectUrl+"updateDoctor",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
formdata = new FormData();
//self.doctor(new Doctor());
}
});
});
所以现在我没有在表单字段中获得任何值,甚至在浏览器控制台中也没有错误。
任何人都可以指出我所做的错误吗?
最佳答案
这是因为ajax请求到viewmodel Patientp的数据初始化,
尝试将 Patientp 更改为此。
<table id="test">
<tr>
<td>Name</td>
<td><input type="text" name="name" data-bind='value: person.name' /></td>
</tr>
<tr>
<td>Department</td>
<td><select data-bind='options: departmentOptions, optionsText: departmentOptions, optionsCaption: "Select...", value: person.department'> </select></td>
</tr>
<tr>
<td>Gender</td>
<td><select data-bind='options: genderOptions, optionsText: genderOptions, optionsCaption: "Select...", value: person.gender'> </select></td>
</tr>
<tr>
<td>Degree</td>
<td><select data-bind='options: degreeOptions, optionsText: degreeOptions, optionsCaption: "Select...", value: person.degree'> </select></td>
</tr>
<tr>
<td>UserName</td>
<td><input type="text" name="name" data-bind='value: person.username' /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="name" data-bind='value: person.password' /></td>
</tr>
</table>
var Patientp = function () {
this.id = ko.observable('');
this.name = ko.observable('');
this.degree = ko.observable('');
this.gender = ko.observable('');
this.username = ko.observable('');
this.password = ko.observable('');
this.email = ko.observable('');
this.mobile = ko.observable('');
this.imgFile = ko.observable('');
this.imgSrc = ko.observable('');
this.imagePath = ko.observable('');
this.userid = ko.observable('');
this.department = ko.observable('');
}
然后设置如下值:
if (currPat.userid == "${IDis}") {
console.log(currPat.degree);
pat.name = currPat.name;
pat.username = currPat.username;
pat.password = currPat.password;
pat.email = currPat.email;
pat.mobile = currPat.mobile;
pat.gender = currPat.gender;
pat.department = currPat.department;
pat.degree = currPat.degree;
pat.imagePath = currPat.imagePath;
pat.userid = currPat.userid;
pat.id = currPat.id;
}
这是 Demo
关于javascript - 通过 knockout 数据绑定(bind)获取输入字段中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23443824/
我想认真学习更多关于 ES6 的知识。我一直在网上做一些例子,虽然我得到了大部分例子,但有时我什至不知道从哪里开始。请注意,我对这个 ES6 和 Knockout 的东西 super 菜鸟,想通过从他
我正在使用 knockout.mapping 将我的 javascript 对象转换为可观察到的 knockout 。在这个 javascript 对象内部是一个嵌套对象,并且这个嵌套对象正在被转换,
我正在用 Kendo、Knockout 和 kendo-knockoutjs 库做一些实验。我想将 knockout View 模型与剑道数据源一起使用,并将其绑定(bind)到剑道网格小部件。 在剑
有谁知道现在当 jQuery.tmpl is not being actively developed or maintained anymore 时 Knockout 的默认模板引擎会发生什么? K
我面临一个问题,即添加新项目时计算的可观察数组未更新。 self.FilteredCityList = ko.computed(function() { var filteredCollectio
我想对数组中的多个属性使用 this fiddle 中的 ko.utils.arrayGetDistinctValues,因此我将数组映射到我想要的两个属性的数组 viewModel.justCate
我在使用 Knockout 的映射插件时遇到了一个奇怪的问题。 如果我通过映射填充一个 observable 数组,我将无法迭代该数组或获取其长度,即使 UI 已正确更新,该数组似乎也是空的。 你可以
所以我试图让 knockout 与 summernote 打得很好。它并没有真正起作用。我意识到这是因为 Summernote 使用了 而不仅仅是一个输入字段。 我的绑定(bind)是这样的: ko.
另一个我似乎无法找到帮助的 knockout 问题。 我基本上是在尝试实现级联下拉列表。前几天我请求帮助了解如何解析我的复杂 JSON(它来自 cakePHP Controller 。前几天我收到的帮
我在尝试使用映射插件将项目更新为 observableArray 的一部分时遇到问题。 我有以下代码 accounts.list = ko.mapping.fromJS(@(Html.Raw(Mode
有几件事我不明白。 根据 KO 文档(以及关于 SO 的许多帖子),在查询和编写 observable 时应该使用括号。但是在绑定(bind)子属性时,在我看来,是否使用括号并不重要。 = 都返回
我从 WCF 接收数据并将其绑定(bind)到表。我在这个论坛上得到了帮助,添加了一些计算字段,一切都很完美。我想在此表的页脚添加总计。我的页面的简单版本可以在 http://jsfiddle.net
我有一个使用 Knockout 显示过滤列表的页面。 View 模型有一个对象数组,以及一个使用 ko.utils.arrayFilter 来过滤数组的 ko.computed。 有时过滤会变得非常复
我正在使用 jquery quicksearch 来搜索由 knockout foreach 循环填充的表。 需要启动快速搜索元素 foreach 结束后 . 我尝试了几种方法,但到目前为止都没有成功
我正在尝试使用类似于 silverlight 中的数据表单的数据网格来创建数据表单功能。 我有一个 div 来显示从网格中选择的项目: $('#readDate').attr('data-bind',
我有一个带有分页功能的 View 模型,它维护 pageIndex 属性。 Viewmodel 有一个包含页面的页面数组。每个页面都有自己的 pageNumber 属性。 我的 View 模型的分页代
名称+复选框的单向绑定(bind)工作正常,但它最初不适用于单选按钮employeeTypeA,尽管它在viewmodel中的值为true,html显示单选按钮未设置,为什么会这样?
我正在尝试解决ko映射的一个小问题。场景是我的ViewModel基本上是对象的集合。每个对象都是通过json调用创建的,方法是: var ViewModel = ko.observableArray(
使用 computed 的主要区别是什么?函数和 subscribe在knockout.js 中的功能? 最佳答案 计算出的 observable 通常用于返回计算出的值。作为计算评估的一部分访问的任
我有带有项目验证的ko.observableArrays。用户可以将项目标记为已删除。将其标记为已删除时,我需要在该项目上禁用验证。 您如何动态禁用验证? 示例:http://jsfiddle.net
我是一名优秀的程序员,十分优秀!