2017-04-19 54 views
-2

我是JavaScript新手,所以我没有那么多的想法,这是最好的方法。如何在刷新页面时在HTML外部保存数据?

这是我的HTML代码:

<!doctype html> 
<html lang="en"> 
<html> 
<head> 
<title>System Links</title> 
<link rel="stylesheet" type="text/css" href="graphics.css" /> 
<script type="text/javascript" src="Brain.js"></script> 
</head> 
<body> 
<h1 id="hello">Test-Page-Production</h1> 
<div id="button2" ><a href="FrontPage.html">Production</a></div> 
<div id="button2"><a href="SecondPage.html">NonProduction</a></div> 


<table id= "Hello" style="height:;width:100%; position: absolute; top: 200px; bottom: 0; left: 0; right: 0;border:1px solid" > 
    <thead> 
    <tr> 
    <th>CheckBox</th> 
    <th>Host</th> 
    <th>SID</th> 
    <th>NR</th> 
    <th>SCS</th> 
    <th>Type</th> 
    <th>URL</th> 
    <th>SAP Components</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr > 
    <td><INPUT type="checkbox" name="chk"/></td> 
    <td>Host Name</td> 
    <td>SID</td> 
    <td>4</td> 
    <td>17</td> 
    <td>ABAP</td> 
    <td><a href="https://www.facebook.com/">Links</a></td> 
    <td>HanaSystem</td></tr> 

    <tr> 
    <td><INPUT type="checkbox" name="chk"/></td> 
    <td>Host Name</td> 
    <td>SID</td> 
    <td>4</td> 
    <td>17</td> 
    <td>ABAP</td> 
    <td>Links</td> 
    <td>HanaSystem</td> 
    </tr> 


    <tr> 
    <td><INPUT type="checkbox" name="chk"/></td> 
    <td>Host Name</td> 
    <td>SID</td> 
    <td>4</td> 
    <td>17</td> 
    <td>ABAP</td> 
    <td>Links</td> 
    <td>HanaSystem</td> 
    </tr> 
    </tbody> 
</table> 

<button id="add" onClick="addRow()"> 
Add Row</button> 


<button id="delete" onClick="deleteCell()"> 
Delete Row</button> 

</body> 
</html> 


This is my javascript code: 

function addRow() 
{ 

var Host=prompt("Enter Host Name"); 
var SID=prompt("Enter System ID"); 
var NR=prompt("Enter the NRsd:"); 
var SystemNumber=prompt("Enter System Number"); 
var Type=prompt("Enter the Type"); 
var URL=prompt("Enter the URL"); 
var SystemComponents=prompt("Enter the System Components"); 

       //tableBody=document.getElementsByTagName("tbody").item(0); 
       var tableBody = document.getElementById('Hello').getElementsByTagName('tbody')[0]; 
       //row=document.createElement("tr"); 
       var row = tableBody.insertRow(tableBody.rows.length); 


       var newCell0 =row.insertCell(0); 
       var cb = document.createElement('input'); 
       var cb=document.createElement('input'); 
       cb.type = 'checkbox'; 
       newCell0.appendChild(cb); 


       // Insert a cell in the row at index 0 
       var newCell = row.insertCell(1); 
       // Append a text node to the cell 
       var newText = document.createTextNode(Host); 
       newCell.appendChild(newText); 


       // Insert a cell in the row at index 0 
       var newCell1 = row.insertCell(2); 
       // Append a text node to the cell 
       var newText1 = document.createTextNode(SID); 
       newCell1.appendChild(newText1); 



       // Insert a cell in the row at index 0 
       var newCell2 = row.insertCell(3); 
       // Append a text node to the cell 
       var newText2 = document.createTextNode(NR); 
       newCell2.appendChild(newText2); 




       // Insert a cell in the row at index 0 
       var newCell3 = row.insertCell(4); 
       // Append a text node to the cell 
       var newText3 = document.createTextNode(SystemNumber); 
       newCell3.appendChild(newText3); 



       // Insert a cell in the row at index 0 
       var newCell4 = row.insertCell(5); 
       // Append a text node to the cell 
       var newText4 = document.createTextNode(Type); 
       newCell4.appendChild(newText4); 



       // Insert a cell in the row at index 0 
       var newCell5 = row.insertCell(6); 
       // Append a text node to the cell 
       var newText5 = document.createTextNode(URL); 
       newCell5.appendChild(newText5); 


       // Insert a cell in the row at index 0 
       var newCell6= row.insertCell(7); 
       // Append a text node to the cell 
       var newText6 = document.createTextNode(SystemComponents); 
       newCell6.appendChild(newText6); 

} 

function deleteCell(){ 


//var tableBody = document.getElementById('Hello').getElementsByTagName('tbody')[0]; 
//document.getElementById('Hello').getElementsByTagName('tbody').deleteRow(4); 

//var confirmation=window.confirm("Are you sure you want to delete"); 

var tableBody = document.getElementById('Hello').getElementsByTagName('tbody')[0]; 
var confirmation=window.confirm("Are you sure you want to delete"); 
var rowCount = tableBody.rows.length; 
if(confirmation==true){ 
for(var i=0;i<rowCount;i++){ 
    var row = tableBody.rows[i]; 
    var chkbox = row.cells[0].childNodes[0]; 
    if(null != chkbox && true == chkbox.checked) { 
        tableBody.deleteRow(i); 
        rowCount--; 
        i--; 
       } 
} 
} 


} 

当用户按下按钮addrow数据应该得到saved.So如何在我的情况下,最精确的way.Since实现这一whenver用户点击刷新按钮该页面从头开始再次刷新而不保存任何数据。

+0

HTML是在客户端。每当刷新页面时,服务器都会返回相同的未更改数据。您需要向服务器发送请求并保存更改。 –

+0

你的问题是'为了外部保存数据',所以你必须知道你不能在客户端HTML页面拥有持久数据。你想要如何在外部存储它的建议吗?如果是这样的话,这个问题可能会被过于宽泛地关闭。你能否提供更多关于你正在使用的服务器端技术的信息(如果有的话)? – SeanOB

+0

使用javascript ajax将'json'数据发送到服务器(后端代码)并将数据保存到那里。您可以进行回传来检查或加载新数据并相应地更新您的HTML标记。 – ThatAwesomeCoder

回答

0

您可以将数据存储在浏览器的localStorage或sessionStorage中。

1)点击添加按钮后,保存日期localStorage/sessionStorage。 2)页面重新加载后,从localStorage/sessionStorage中检索数据并通过javascript填充表格。

要保存一个项目: sessionStorage.setItem('key','value');

检索项目 var data = sessionStorage.getItem('key');

API参考这里: https://developer.mozilla.org/en/docs/Web/API/Window/sessionStorage

+0

如果我使用本地存储,然后是否每个客户端都可以动态添加数据,以及对于每个客户端是否显示由其他客户端添加的数据,那么我有这个问题? –

+0

是的,每个客户端都可以动态添加数据。但是每个客户端只会看到他们自己的数据(因为它是由他们自己的浏览器本地存储的)。 – SeanOB

+0

但我需要充分的灵活性,以便每个客户端都可以看到其他客户端添加的数据。那么有没有办法做到这一点? –

相关问题