2015-05-29 72 views
0

這是用來添加學生如何使用HTML,CSS和JavaScript將索引添加到本地存儲?

<!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的頁面的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; 
} 

我我不太確定我做錯了什麼,所以解釋也會很棒!謝謝! :)

+0

問題在哪裏? –

+0

歡迎來到堆棧溢出。你的問題包含太多的代碼。你的頭銜很好,但我們無法理解你準備做什麼。請嘗試僅發佈與問題代碼相關的信息。 – Ifch0o1

+0

爲什麼不使用localstorage來存儲對象數組而不是執行「setItem」爲什麼不創建一個名爲:* Entry *然後是'var list = GetItem(「data」)的對象。 SetItem(「Data」,list);'或更好:'localStorage.setItem(「data」,localStorage.getItem(「data」)。push(new Entry(i,f,l,num));' – Fallenreaper

回答

0

問題是如何使用HTML,CSS和JavaScript將索引添加到本地存儲?

編碼明智的HTML5和CSS與它無關,除了顯示結束值。

這裏有一個小提琴:http://jsfiddle.net/f3aej450/2/

的JavaScript(onload事件):

//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只是在那裏覆蓋或刪除它。

相關問題