m0_71681226 2022-10-24 22:53 采纳率: 100%
浏览 41
已结题

js数组存储数据,在html添加数据时undefined

js数组存储数据,在html添加数据时undefined

数组:


```javascript
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:

Add a new patient
Patient ID*:<input type="text" id="newId" value="">
First name*:<input type="text" id="newFirstName" value="">
Last name*:<input type="text" id="newLastName" value="">
Date of birth *:<input type="text" id="newDateOfBirth" value="">
Gender*:<input type="text" id="newGender" value="">
Primary insurance*:<input type="text" id="newPrimaryInsurance" value="">
Address *:<input type="text" id="newAddress" value="">
Contact number *:<input type="text" id="newContactNumber" value="">
Next of kin:<input type="text" id="newNextOfKin" value="">
<input type="button" value="Add a new Patient" onclick="addNewPatient();">

Javascript:

function addNewPatient(){
    
     var newId=document.getElementById("newId");
     var newFirstName=document.getElementById("newFirstName");
     var newLastName=document.getElementById("newLastName");
     var newDateOfBirth=document.getElementById("newDateOfBirth");
     var newGender=document.getElementById("newGender");
     var newPrimaryInsurance=document.getElementById("newPrimaryInsurance");
     var newAddress=document.getElementById("newAddress");
     var newContactNumber=document.getElementById("newContactNumber");
     var newNextOfKin=document.getElementById("newNextOfKin");
     
     var newPatient=new Array(newId.value,newFirstName.value,newLastName.value,newDateOfBirth.value,newGender.value,newPrimaryInsurance.value,newAddress.value,newContactNumber.value,newNextOfKin.value);
     list.push(newPatient);
     
     refreshList();
}

错误:

img

请问改怎么解决这个问题

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-10-24 23:01
    关注

    显示的时候用的json对象,加入到list集合中也要json对象才行

    
            function addNewPatient() {
    
                var newId = document.getElementById("newId");
                var newFirstName = document.getElementById("newFirstName");
                var newLastName = document.getElementById("newLastName");
                var newDateOfBirth = document.getElementById("newDateOfBirth");
                var newGender = document.getElementById("newGender");
                var newPrimaryInsurance = document.getElementById("newPrimaryInsurance");
                var newAddress = document.getElementById("newAddress");
                var newContactNumber = document.getElementById("newContactNumber");
                var newNextOfKin = document.getElementById("newNextOfKin");
    
                var newPatient = {
                    Id: newId.value,
                    FirstName: newFirstName.value,
                    LastName: newLastName.value,
                    DateOfBirth: newDateOfBirth.value,
                    Gender: newGender.value,
                    PrimaryInsurance: newPrimaryInsurance.value,
                    Address: newAddress.value,
                    ContactNumber: newContactNumber.value,
                    NextOfKin: newNextOfKin.value
                }; 
                list.push(newPatient);
    
                refreshList();
            }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月24日
  • 已采纳回答 10月24日
  • 创建了问题 10月24日

悬赏问题

  • ¥15 在codeblocks里主函数运行到打印生成的链表就结束了 没有再往下是为什么呢
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!
  • ¥20 win11账户锁定时间设为0无法登录
  • ¥45 C#学生成绩管理系统
  • ¥30 matlab appdesigner私有函数嵌套整合