填写用户名和姓氏并转到下一页

我不知道如何连接将姓名和姓氏保存在本地存储中并转到其他页面的功能。如果您能指出问题,我将非常感激:)


index.html

<form id="from" action="" method="post">

    <div>

        <pre> <label for="name">Име</label></pre>

        <input id="name" name="name" type="text"  required>

        <br>

        <pre><label for="surname"> Презиме </label></pre>

        <input id="surname" surname="surname" type="text"  required>

        <br><br>

    </div>

    <a href="quiz.html"><button onclick="sumbit()"> Започни тест </button> </a> 

</form>




script.js

let infs = [];

    const addInf= (ev)=>{

        ev.preventDefault();  //to stop the form submitting

        let inf = {

            id: Date.now(),

            name: document.getElementById('name').value,

            surname: document.getElementById('surname').value

        }

        infs.push(inf);

        document.forms[0].reset(); // to clear the form for the next entries


        //saving to LS

        localStorage.setItem('MyInfList', JSON.stringify(infs) );

    }

    document.addEventListener('DOMContentLoaded', ()=>{

        document.getElementById('btn').addEventListener('click', addInf);

    });


德玛西亚99
浏览 94回答 1
1回答

子衿沉夜

试试这个,使用 window.location 重定向到另一个页面,它具有与 href 相同的功能。<form id="from" action="" method="post">&nbsp; <div>&nbsp; &nbsp; <pre> <label for="name">Име</label></pre>&nbsp; &nbsp; <input id="name" name="name" type="text" required>&nbsp; &nbsp; <br>&nbsp; &nbsp; <pre><label for="surname"> Презиме </label></pre>&nbsp; &nbsp; <input id="surname" surname="surname" type="text" required>&nbsp; &nbsp; <br><br>&nbsp; </div>&nbsp; <a><button id="btn"> Започни тест </button> </a></form><script>&nbsp; let infs = [];&nbsp; const addInf = (ev) => {&nbsp; &nbsp; ev.preventDefault(); //to stop the form submitting&nbsp; &nbsp; let inf = {&nbsp; &nbsp; &nbsp; id: Date.now(),&nbsp; &nbsp; &nbsp; name: document.getElementById('name').value,&nbsp; &nbsp; &nbsp; surname: document.getElementById('surname').value&nbsp; &nbsp; }&nbsp; &nbsp; infs.push(inf);&nbsp; &nbsp; //saving to LS&nbsp; &nbsp; localStorage.setItem('MyInfList', JSON.stringify(infs));&nbsp; &nbsp; document.forms[0].reset(); // to clear the form for the next entries&nbsp; &nbsp; window.location = "quiz.html"&nbsp; }&nbsp; document.addEventListener('DOMContentLoaded', () => {&nbsp; &nbsp; document.getElementById('btn').addEventListener('click', addInf);&nbsp; });</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript