document.getElementById 始终返回 null

拉斐尔·布拉德利

我有这段代码:

<form class="register" id="reg-form">
        <input type="text" class="input-box" placeholder="Username" id="username" required><br><br>
        <input type="text" class="input-box" placeholder="Full Name" id="name" required><br><br>
        <input type="email" class="input-box" placeholder="Email" id="email" required><br><br>
        <input type="password" class="input-box" placeholder="Password" id="password" required><br><br>
        <input type="password" class="input-box" placeholder="Repeat Password" id="passwordrep" required><br><br><br>
        <button class="btn-rounded-md black" id="reg-btn">Register</button>
    </form>

和这个js:

window.onload = function() {
  var email = document.getElementById('email').value;
  var password = document.getElementById('password').value;
  var reppassword = document.getElementById('passwordrep').value;

  document.getElementById('reg-form').addEventListener('submit', function(e) {
    e.preventDefault();
    if (password == reppassword) {
      console.log("Passwords match. Submitting...");

      auth.createUserWithEmailAndPassword(email, password).then(userCredential => {
        console.log('Signed up!');
      });
    }
  });
}

但是在 js 开始时创建的所有变量都返回空值。有任何想法吗?

某些表演

您正在检索页面加载上的值,而不是提交时;它们将始终为空(不是 null,而是空字符串)。改成:

document.getElementById('reg-form').addEventListener('submit', function(e) {
  e.preventDefault();
  var email = document.getElementById('email').value;
  var password = document.getElementById('password').value;
  var reppassword = document.getElementById('passwordrep').value;

  if (password == reppassword) {
    console.log("Passwords match. Submitting...");

    auth.createUserWithEmailAndPassword(email, password).then(userCredential => {
      console.log('Signed up!');
    });
  }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

document.getElementById()返回null

document.getElementById返回null

JavaScript getElementById始终返回null

Javascript Document.getElementById返回null

离子2 document.getElementById返回null

所有 document.getElementById('') 返回 null

document.getelementbyid在循环中返回null

为什么document.getElementById在这里返回null?

无法使用document.getElementById获取元素,返回null

Android WebView始终在loadUrl上为javascript getElementById返回null

尝试查找对输入表单的响应时,getElementById 始终返回 null

getElementbyID返回Null

getElementById(...)意外返回null

document.getElementById返回空

document.getElementById 返回 HTMLDivElement {}

从document.getElementById()。value获取null

Ajax Uncaught TypeError: document.getElementById(...) is null

如果未定义element,document.getelementbyId将返回null吗?

如果具有给定ID的元素存在,则document.getelementbyid('ID')方法返回null

开玩笑+酶,使用mount(),document.getElementById()在_method调用后出现的组件上返回null

Vuejs + Google Maps> document.getElementById,this.refs为div返回null

document.getElementById(“ elementId”)。innerHTML =()的倍数,导致第二个返回为null

当存在具有该 ID 的元素时,为什么 document.getElementById() 返回 null?

JavaScript getElementById返回null错误?

为什么getElementById返回null

getElementById返回null?(使用Vue)

XmlDocument.GetElementById()返回null

无论我尝试使用哪种对象/网站,GetElementById始终返回null

TypeError:document.getElementById(...)为null不会停止出现