为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

德玛西亚99 2022-07-21 09:54:00
我不知道如何连接将姓名和姓氏保存在本地存储中并转到其他页面的功能。如果您能指出问题,我将非常感激:)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.jslet 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);    });
查看完整描述

1 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

试试这个,使用 window.location 重定向到另一个页面,它具有与 href 相同的功能。


<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><button id="btn"> Започни тест </button> </a>

</form>



<script>

  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);

    //saving to LS

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

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

    window.location = "quiz.html"

  }

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

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

  });

</script>


查看完整回答
反对 回复 2022-07-21
  • 1 回答
  • 0 关注
  • 93 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号