【问题标题】:I can't get my form data into an array to display in a table我无法将表单数据放入数组中以显示在表格中
【发布时间】:2015-01-19 14:50:46
【问题描述】:

我有一个网页,上面有一个表格,用于输入患者信息(当然是所有虚拟数据)。我设置了表单和表格来显示提交的信息。当用户单击“添加患者”按钮时,我似乎无法弄清楚为什么数据没有存储在数组中。我尝试多次提交数据,然后我使用“console.log(patientArray)”检查数组,它说我尝试存储的信息是未定义的。

这是我的网页:

@using OnboardingProject.App_Code
@using OnboardingProject.Controllers

@{
    ViewBag.Title = "Patients";
}

<div class="title">
    <div>
        <h1 style="float: left">@ViewBag.Title</h1>
    </div>
    <div class="rmm" style="float: right; display: inline-block">
        <ul>
            <li><button id="NewPatient">New Patient</button></li>
        </ul>
    </div>
</div>
<div id="modal_content">
    <div id="modal_window" title="Complete the form below to add a new patient:">
        <div style="text-align: right;"><a id="modal_close" href="#">close <b>X</b></a></div>

        <form id="add_patient" method="POST" action="#" accept-charset="UTF-8">
        <p><label>First Name<strong>*</strong><br>
        <input type="text" autofocus required size="48" id="fname" value=""></label></p>
        <p><label>Last Name<strong>*</strong><br>
        <input type="text" autofocus required size="48" id="lname" value=""></label></p>
        <p><label>Birthdate (mm/dd/yyyy)<strong>*</strong><br>
        <input type="text" autofocus required size="48" id="bday" value=""></label></p>
        <p><label>Site Name<strong>*</strong><br>
        <input type="text" autofocus required size="48" id="location" value=""></label></p>
        <p><label>SSN<strong>*</strong><br>
        <input type="text" autofocus required size="48" id="pat_ssn" value=""></label></p>
        <p><input type="button" value="Add Patient" onclick="addPatient()"></p>
        </form>
    </div>
</div>
<div class="content">
    <div id="patient_table">
        <table id="patients">
            <tr>
                <th id="p_name">Patient Name</th>
                <th id="p_site">Site</th>
                <th id="dob">Date of Birth</th>
                <th id="ssn">SSN</th>
                <th id="edits"></th>
            </tr>
        </table>
    </div>
</div>
<script src="@Url.Content("~/Scripts/PatientInfo.js")" type="text/javascript></script>

这是我的 javascript 文件:

//Display the modal when New Patient button is clicked
$("#NewPatient").click(function () {
    $("#modal_window").show();
});

//Hide the modal when close is clicked
$("#modal_close").click(function () {
    $("#modal_window").hide();
});

//Create an array to store Patient Information
var patientArray = [];

var fName = document.getElementById("fname").value;
var lName = document.getElementById("lname").value;
var bDate = document.getElementById("bday").value;
var sName = document.getElementById("location").value;
var SSN = document.getElementById("pat_ssn").value;

function addPatient( fName, lName, bDate, sName, SSN ) {
    patientArray.push(fName, lName, bDate, sName, SSN);
    $("#modal_window").hide();
}

//Create a table from the array
var table = document.getElementById("patients");

var tbody = document.createElement("tbody");
table.appendChild(tbody);
patientArray.forEach(function (items) {
    var row = document.createElement("tr");
    items.forEach(function (item) {
        var cell = document.createElement("td");
        cell.textContent = item;
        row.appendChild(cell);
    });
    tbody.appendChild(row);
});

【问题讨论】:

  • 您试图在页面加载时获取值,甚至在用户与元素交互之前。此外,如果代码在 head 中运行,如果不使用负载处理程序,元素本身甚至都不存在。
  • 代码没有在头部运行。对脚本文件的调用在 html 文件的底部完成。

标签: javascript jquery html arrays html-table


【解决方案1】:

html 按钮调用函数 addPatient()。请注意,调用 addPatient 函数与执行前五行(函数外部)无关,这是将值提取到变量中的地方。这就是为什么当函数被调用时,什么也没有发生。此外,该函数在其定义中需要 5 个参数。然后您必须使用 5 个参数调用该函数。或者将获取值的行为放在函数内部,并在没有参数的情况下调用。

换句话说,这里有两种方法: 选项 1:

将此代码移到函数 addPatient 中。 删除函数的参数定义,使其成为没有参数的函数。

var fName = document.getElementById("fname").value;
var lName = document.getElementById("lname").value;
var bDate = document.getElementById("bday").value;
var sName = document.getElementById("location").value;
var SSN = document.getElementById("pat_ssn").value;

选项 2:

保持 addPatient 函数不变。但是,创建另一个函数,例如 Fetch_And_Add_Patient(),它具有以下代码:

function Fetch_Add_Patient() {
    var fName = document.getElementById("fname").value;
    var lName = document.getElementById("lname").value;
    var bDate = document.getElementById("bday").value;
    var sName = document.getElementById("location").value;
    var SSN = document.getElementById("pat_ssn").value;
    addPatient(fName, lName, bDate, sName, SSN);
}

然后在HTML按钮中,调用上面的函数,不带任何参数,而不是调用addPatient,不带任何参数。

<p><input type="button" value="Add Patient" onclick="addPatient()"></p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    相关资源
    最近更新 更多