<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SwinTech</title>
</head>
<body>
<form action="apply.html" method="post">
<div class="consultantinformation">
<h2> Job reference number: </h2>
<br>
<h4 id="jobreference2"> 6LZ9W </h4>
</div>
<button id="submitbutton">Submit</button>
</form>
<script>
document.getElementById("submitbutton").addEventListener("click", setItem);
localStorage.setItem("jobreference1", "1FN43");
</script>
</body>
</html>
我正在嘗試使用本地存盤將“jobreference1”從第一個片段轉移到第二個片段。如您所見,我已將其存盤在一個外部 JavaScript 檔案中并將其發送到另一個檔案。但是,當我嘗試在第二個代碼段中的 HTML 代碼中呼叫它時,該值已轉移到該代碼段中,但它根本不起作用。我決定將我所有的 HTML 和 JavaScript 代碼包含在我的第二個代碼段中,因為我相信這就是問題所在。謝謝你。
注意:沒有行內 JavaScript 或 jQuery。
uj5u.com熱心網友回復:
這是一個作業示例。該index.html設定在頁面加載的參考。表單提交重定向到apply.html并在頁面加載時讀取正確的值。
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SwinTech</title>
</head>
<body>
<form action="apply.html" method="post">
<div class="consultantinformation">
<h2> Job reference number: </h2>
<br>
<h4 id="jobreference2"> 6LZ9W </h4>
</div>
<button>Submit</button>
</form>
<script>
localStorage.setItem("jobreference1", "1FN43");
</script>
</body>
</html>
申請.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p>Job Reference Number: <span id="job1"></span></p>
<script>
document.getElementById("job1").innerHTML = localStorage.getItem("jobreference1");
</script>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/311246.html
標籤:javascript html 形式
上一篇:如何從Angular形式修剪值?
