无法使用本地存储中的JavaScript填充电子邮件字段

时间:2018-07-14 09:36:41

标签: javascript html

我有两个文件test.html和test2.html,我能够保存表单的值,但不能填写test2.html表单中的输入字段。

Test.html

    <script  type="text/javascript">
  function store(){
     var inputEmail= document.getElementById("email");

     localStorage.setItem("email", inputEmail.value);
    }
</script>
<form action="test2.html" class="form-login"  method="post" /> 
<input name="email" type="email" id="email" required="" placeholder="Email" />

<button onclick="store()" type="button">StoreEmail</button>

Test2.html

      <script type="text/javascript">
      function get(){

         var storedValue = localStorage.getItem("email");

         document.getElementById("email").innerHTML = storedValue;
         document.getElementById("email").value = storedValue;

        }
    </script>
    <p id="email">qqqqqq</p>
    <form action="" class="form-login"  method="post" /> 
    <input name="email" type="email" id="email" placeholder="Email" />

<button onclick="get()" type="button">getEmail</button>

当我单击getEmail按钮时,段落会更改,但是Email输入字段不会随值更改。请帮忙。

1 个答案:

答案 0 :(得分:3)

每个DOM节点的ID在整个DOM中应该是唯一的

document.getElementById始终返回第一个匹配的dom节点