js用splice更新数组失败。
数组:
var list = [{ "Id": '0', "FirstName": "Stark", "LastName": "Bill", "DateOfBirth": "2000/10/11", "Gender": "Male", "PrimaryInsurance": "Medicare", "Address": "ddadsdasd", "ContactNumber": "1338872818738", "NextOfKin": "weqe" },
{ "Id": '1', "FirstName": "Shown", "LastName": "Jenny", "DateOfBirth": "2000/10/11", "Gender": "Male", "PrimaryInsurance": "Medicare", "Address": "ddadsdasd", "ContactNumber": "1338872818738", "NextOfKin": "weqe" },
{ "Id": '2', "FirstName": "Geogrge", "LastName": "Hash", "DateOfBirth": "2000/10/11", "Gender": "Male", "PrimaryInsurance": "Medicare", "Address": "ddadsdasd", "ContactNumber": "1338872818738", "NextOfKin": "weqe" },
{ "Id": '3', "FirstName": "Intwre", "LastName": "zaiwoo", "DateOfBirth": "2000/10/11", "Gender": "Male", "PrimaryInsurance": "Medicare", "Address": "ddadsdasd", "ContactNumber": "1338872818738", "NextOfKin": "weqe" },
{ "Id": '4', "FirstName": "Niko", "LastName": "baby", "DateOfBirth": "2000/10/11", "Gender": "Male", "PrimaryInsurance": "Medicare", "Address": "ddadsdasd", "ContactNumber": "1338872818738", "NextOfKin": "weqe" }];
html:
Insert a patient
Patient ID*:<input type="text" id="insertId" value="">
First name*:<input type="text" id="FirstName" value="">
Last name*:<input type="text" id="LastName" value="">
Date of birth *:<input type="text" id="DateOfBirth" value="">
Gender*:<input type="text" id="Gender" value="">
Primary insurance*:<input type="text" id="PrimaryInsurance" value="">
Address *:<input type="text" id="Address" value="">
Contact number *:<input type="text" id="ContactNumber" value="">
Next of kin:<input type="text" id="NextOfKin" value="">
<input type="button" value="Insert a Patient" onclick="insertPatient();">
js:
function insertById(list, insertId) {
var insertId=document.getElementById("insertId");
var FirstName=document.getElementById("FirstName");
var LastName=document.getElementById("LastName");
var DateOfBirth=document.getElementById("DateOfBirth");
var Gender=document.getElementById("Gender");
var PrimaryInsurance=document.getElementById("PrimaryInsurance");
var Address=document.getElementById("Address");
var ContactNumber=document.getElementById("ContactNumber");
var NextOfKin=document.getElementById("NextOfKin");
for (var i = 0; i < list.length; i++) {
if (list[i].Id === deleteId) {
list.splice(i, 1,{Id: insertId.value,
FirstName: FirstName.value,
LastName: LastName.value,
DateOfBirth: DateOfBirth.value,
Gender: Gender.value,
PrimaryInsurance: PrimaryInsurance.value,
Address: Address.value,
ContactNumber: ContactNumber.value,
NextOfKin: NextOfKin.value});
}
}
return list;
}
function insertPatient(){
var insertId=document.getElementById("insertId");
var res = insertById(list,insertId.value)
console.log(res)
refreshList();
}
错误:
点击insert按钮控制台数组刷新了一下但并没有更新数据。
请问这个问题该怎么解决