[英]Passing form data from one webpage to another
我試圖使用以下方法將表單數據從一個網頁發送到另一個網頁:將數據從一個網頁傳遞到另一個網頁
我正在粘貼我創建的函數的 javascript 代碼:
function store(){
window.localStorage.setItem("name",document.getElementById("name").value);
window.localStorage.setItem("email",document.getElementById("email").value);
window.localStorage.setItem("tele",document.getElementById("tele").value);
window.localStorage.setItem("gender",document.getElementById("gender").value);
window.localStorage.setItem("comment",document.getElementById("comments").value);
window.location.href = "contact.html";
}
但問題是window.location.href
不起作用並且網頁沒有重定向。
我已經看到了控制台的錯誤,但沒有任何錯誤。
誰能告訴我錯在哪里?
編輯 1:驗證功能:
function validate(){
var comment = document.getElementById("comments").value;
var gender = document.getElementById("gender").value;
var len = comment.length;
if (len > 100)
{
alert("Reduce the length of the comment less than 100 characters");
}
else if (gender == "Select Gender")
{
alert("Please Select a gender");
}
else {
store();
}
}
表單的html:
<form class="form-horizontal" role="form">
提交按鈕:
<input type="submit" id="submit" value="Submit" onclick="validate()">
看看這個plunker:
https://plnkr.co/edit/P4XqhYciFxZeYlRbWXtd?p=preview
<form class="form-horizontal" role="form">
<textarea rows="4" cols="50" id="comments" placeholder="Enter comments"></textarea>
<br />
<select id="gender">
<option value="Select Gender">Select Gender</option>
<option value="male">male</option>
<option value="female">female</option>
</select>
<br />
<button type="button" id="submit" onclick="validate()">Go</button>
</form>
我已經更新了plunker。 頁面正在重定向,您可以看到值保存在本地存儲中。 它工作正常。
也許您因為輸入提交而遇到問題,我改用了一個按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.