无法读取null,注册表单的属性“value”

时间:2014-02-01 20:31:10

标签: javascript html

我已经查看过有关同一问题的许多问题,但我仍然无法弄清楚我的代码有什么问题。

HTML:

<form id="form" method="POST" onsubmit="return false;">
    <input type="text" placeholder="First Name" id="first" /><br />
    <input type="text" placeholder="Last name" id="last" /><br />
    <input type="text" placeholder="Username" id="user" /><br />
    <input type="text" placeholder="Email" id="email" /><br />
    <input type="password" id="right" placeholder="Password" id="password" /><br />
    <input type="password" id="right" placeholder="Confirm Password" id="confirm" /><br />
    <button class="btn btn-block btn-lg" method="POST" onclick="signup()">Sign Up</button>
</form>

Javascript:

function signup() {
var f = document.getElementById("first").value;
var l = document.getElementById("last").value;
var user = document.getElementById("user").value;
var e = document.getElementById("email").value;     
var p = document.getElementById("password").value;
var p1 = document.getElementById("confirm").value;
if (f == "" || l == "" || user == "" || e == "" || p == "" || p1 == "") {
  alert("One or more fields are empty!");
  } else {
    alert("test");
  }
}

谢谢大家。

3 个答案:

答案 0 :(得分:3)

<input ... id="right" ... id="password" />

密码中的双重ID&amp;确认密码输入。

答案 1 :(得分:1)

这是因为您在密码输入上设置了两个ID。

<input type="password" id="right" placeholder="Password" id="password" />

浏览器只读取第一个(id =“right”),因此不存在id为“password”的节点。当您尝试使用.getElementById()

时“确认”

答案 2 :(得分:0)

您使用两个ID作为单个字段密码并尝试按ID获取它。 删除id =&#34;对&#34;来自密码字段