localStorage无法正确存储数据

时间:2019-03-13 22:18:06

标签: local-storage

  

我们有一个网站开发任务,我似乎无法得到   本地存储正常工作,我正在尝试存储表值   并在我使用localStorage重新打开页面后检索它们。有东西吗   我的功能有问题吗?对于我一直在玩的凌乱代码,我深表歉意   它试图解决它一段时间。这是   .js和.html代码:

var button = document.getElementById("calcButton");
var rowInsertIncrement = 0;

function grossPayCalculation(){
    var hoursWorked = document.getElementById("hoursWorked").value;
    var hourlyRate = document.getElementById("hourlyRate").value;

    if (hoursWorked <= 40){
        return hoursWorked*hourlyRate;
    }
    else {
        return (40*hourlyRate) + (hoursWorked - 40) * (hourlyRate*1.5);
    }
}

function taxCalculation(){
    if (grossPayCalculation() < 250){
        return 0.25;
    }
    else if (grossPayCalculation() >= 250 || grossPayCalculation() < 500){
        return 0.30;
    }
    else if (grossPayCalculation() >= 500 || grossPayCalculation() <=750){
        return 0.40;
    }
    else if (grossPayCalculation() > 750){
        return 0.50;
    }
}

function netPayCalculation(){
    return grossPayCalculation() - grossPayCalculation() * taxCalculation();
}

function insertToTable() {
    let fullName = document.getElementById("fullName").value;
    var hoursWorked = document.getElementById("hoursWorked").value;
    var hourlyRate = document.getElementById("hourlyRate").value;

    if(hoursWorked > 0 && hourlyRate > 0){

    var table = document.getElementById("list");
    var row = table.insertRow(rowInsertIncrement);
    var cellFullName = row.insertCell(0);
    var cellGrossPay = row.insertCell(1);
    var cellTax = row.insertCell(2);
    var cellNetPay = row.insertCell(3);

    cellFullName.innerHTML = fullName;
    cellGrossPay.innerHTML = "$" + grossPayCalculation().toFixed(2);
    cellTax.innerHTML = "$" + (grossPayCalculation() * taxCalculation()).toFixed(2);
    cellNetPay.innerHTML = "$" + netPayCalculation().toFixed(2);

    document.getElementById("payForm").reset();

    document.getElementById("fullName").focus();

    rowInsertIncrement++;

    var tableData = document.getElementByID("list").innerHTML;

    localStorage.setItem("employeeData", tableData);
}

    else {
        alert("Error: Invalid Input");
        document.getElementById("payForm").reset();
        document.getElementById("fullName").focus();
    }

}

document.onload = function() {
    if(localStorage.getItem("employeeData")){
    document.getElementById("list").innerHTML = localStorage.getItem("employeeData");
}
}

button.addEventListener("click", insertToTable, false);

<!doctype html>
<html>
    <head>
        <script language="JavaScript" src="employees.js"></script>
        <link rel="stylesheet" type="text/css" href="employees.css" />
        <meta charset="utf-8" />
        <title>Pay Form</title>
    </head>
    <body>
        <section id="content">
            <h1 id="test2">
                Employee Payroll Entry Form
            </h1>
            <p id="test"></p>
                <form id="payForm">
                    <label for="">Full Name:</label><input type="text" autofocus id="fullName"/><br />
                    <label for="">Hours Worked:</label><input type="text" id="hoursWorked" /><br />
                    <label for="">Hourly Rate:</label><input type="text" id="hourlyRate" />
                    <footer>
                        <button id="calcButton" type="button">Calculate</button>
                    </footer>
                </form>
            <h1>
                Employee Payroll Summary Report
            </h1>

            <table id = "employees">
                <thead>
                    <tr>
                        <th>Employee Name</th>
                        <th>Gross Pay</th>
                        <th>Tax</th>
                        <th>Net Pay</th>
                    </tr>
                </thead>
                <tbody id="list">

                </tbody>
            </table>
        </section>
        <script language="JavaScript" src="employees.js"></script>
    </body>
</html>

0 个答案:

没有答案