简体   繁体   English

将表单数据从一个网页传递到另一个网页

[英]Passing form data from one webpage to another

I was trying to send form data from one webpage to another using this: Passing data from one web page to another我试图使用以下方法将表单数据从一个网页发送到另一个网页:将数据从一个网页传递到另一个网页

I am pasting the javascript code of the function i created:我正在粘贴我创建的函数的 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";
        }

But the Problem is that the window.location.href is not working and the webpage is not redirected.但问题是window.location.href不起作用并且网页没有重定向。

I have seen the console for errors but there aren't any.我已经看到了控制台的错误,但没有任何错误。

Can anyone tell Where is the fault?谁能告诉我错在哪里?

EDIT 1: Validate function:编辑 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();
            }
        }

Form's html:表单的html:

<form class="form-horizontal" role="form">

Submit Button:提交按钮:

<input type="submit" id="submit" value="Submit" onclick="validate()">

Have a look at this plunker:看看这个plunker:

https://plnkr.co/edit/P4XqhYciFxZeYlRbWXtd?p=preview 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>

I have updated the plunker.我已经更新了plunker。 The page is redirecting and you can see that the values are saved in the local storage.页面正在重定向,您可以看到值保存在本地存储中。 It works fine.它工作正常。

Perhaps you had a problem because of your input submit, I used a button instead.也许您因为输入提交而遇到问题,我改用了一个按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM