gpt4 book ai didi

javascript - 如何仅使用 HTML、CSS 和 JavaScript 将索引添加到本地存储?

转载 作者:行者123 更新时间:2023-11-28 07:49:09 26 4
gpt4 key购买 nike

这是用于添加学生的页面的 HTML

<!DOCTYPE html> 
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="function.js"></script>
</head>
<header>
<nav>
<img src="logo.jpg"><h1 id="titt">North Park Clubs</h1>
<li><a href="attendance.html">Attendance</a></li>
</nav>
</header>
<body>
<h1>Add A Student</h1>
<input type="text" id="indx"></input>
<input type="text" id="sFirstName"></input>
<input type="text" id="sLastName"></input>
<input type="text" id="sNumber"></input>
<input type="text" id="sPoints"></input>
<input type="button" onclick="save()" value="Save"></input>
</body>

这是查看考勤页面的HTML

<!DOCTYPE html> 
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script href="functions.js"></script>
</head>
<header>
<nav>
<img src="logo.jpg"><h1 id="titt">North Park Clubs</h1>
<li><a href="attendance.html">Attendance</a></li>
</nav>
</header>
<body onload="load()">
<table id="attendance">
<input type="button" value="Add Student" id="add" link="addStudent.html">

<tr>
<td></td>
<td>First Name</td>
<td>Last Name</td>
<td>Student Number</td>
<td>Points</td>
<td>Absent (If student is present leave this unchecked)</td>
</tr>
<td id="indx"></td>
<td id="sFirstName"></td>
<td id="sLastName"></td>
<td id="sNumber"></td>
<td id="sPoints"></td>
<td><input id="a" type="checkbox"></td>
</table>
</body>

这是我的 JavaScript

var indx = [];
var sFirstName = [];
var sLastName = [];
var sNumber = [];
var sPoints = [];
var a = [];
var attendance = [];
var obj = JSON.parse(attendance);
function save(){
localStorage.setItem("idex", document.getElementById('indx').value);
localStorage.setItem("fName", document.getElementById('sFirstName').value);
localStorage.setItem("lName", document.getElementById('sLastName').value);
localStorage.setItem("numb", document.getElementById('sNumber').value);
}

if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
localStorage.clickcount + " time(s).";
//var fName = localStorage.sFirstName;
//var lName = localStorage.sLastName
//var numb = localStorage,sNumber

//onload document.getElementById('attendance');

function load(){
document.getElementById.idex;
document.getElementById.fName;
document.getElementById.lName;
document.getElementById.numb;
}

我不太确定我做错了什么,所以解释也很好!谢谢! :)

最佳答案

The question is how do I add an index to my localstorage using only HTML, CSS and JavaScript?

编码明智的 HTML5 和 CSS 除了显示最终值外与它无关。

这是一个 fiddle :http://jsfiddle.net/f3aej450/2/

JavaScript(加载):

 //check to see if index exists.
// set it to 0 if it does not,
// otherwise add 1
(localStorage.getItem("index")) ?
localStorage.setItem("index", parseInt(localStorage.getItem('index')) + 1) :
localStorage.setItem("index", 0);

// alert the user to the index number
alert(localStorage.getItem("index"));

作为旁注,我建议使用 sessionStorage 而不是 localStorage - 区别在于 sessionStorage 在浏览器窗口/选项卡关闭时清除。 localStorage 只是坐在那里,直到您覆盖或删除它。

关于javascript - 如何仅使用 HTML、CSS 和 JavaScript 将索引添加到本地存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30536791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com