gpt4 book ai didi

javascript - 类型错误 : document. getElementById(...);在 JavaScript 中为 null

转载 作者:行者123 更新时间:2023-11-27 23:39:42 24 4
gpt4 key购买 nike

我的所有 var 语句都使用标识符:

 var identifier = document.getElementById("somename"); 

那么为什么我会收到空​​错误?我在 Javascript 运行程序中运行此代码并收到 null 错误消息。在我的浏览器 Firefox 和 Chrome 中,我没有收到任何错误或警告。当我在浏览器中运行代码并单击按钮激活事件处理程序时,表单将被清除。无论如何,它不会发送到服务器。这只是练习。我正在学习 javascript 和动态 HTML 类(class)。如果有人愿意查看我的代码并告诉我我做错了什么,我将不胜感激。一定有什么地方我没搞对。这是脚本:

 window.onload = function(){
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var loginName = document.getElementById("uname").value;

var myEmail = document.getElementById("email").value;

var pass1 = document.getElementById("password1").value;
var pass2 = document.getElementById("password2").value;
if(document.getElementById("uname").value == ""){
return false;
alert("Your user name can't be blank.");
};

if(pass1.value !== pass2.value){
get.documentElementById("signin").value.disabled = true;
return false;
alert("Please retype your password.");
}else if(pass1.value === pass2.value){
alert("Welcome!");
};
};

HTML

 <body>
<form action = "" name = "form" method = "Post">
<label for="fname">First Name:</label><input type = "text" id = "fname"required></input>

<label for="lname">Last Name:</label><input type = "text" id = "lname" required></input>

<label for="uname">User Name:</label><input type = "text" id = "uname" required></input><br/>

<label for="password1">Password:</label><input type = "password" id = "password1"required ></input><br/>

<label for="password2">Verify Password:</label><input type = "password" id = "password2"required ></input><br/>

<label for="email">Email Address:</label><input type = "email" id = "email" required></input><br/>

<button type = "submit"id = "signin" onclick = "function()">Submit</button>
</form>
<script src="signUp.js"></script>
</body>

最佳答案

我为你清理了你的代码。有几个地方出现错误(例如,输入 pass1.value 而不是仅 pass1。这应该可行,但当然需要时间研究它以了解我的情况更改并理解原因。Here's a fiddle showing it working. 请注意,您永远不应该期望此类代码在您引用的“运行者”中运行;此处的代码显式引用 DOM 中的特定元素,运行者赢得了这些元素没有。(使用像 JSFiddle 这样的网站更适合此类事情,因为您也可以将 HTML 放入其中)。

var submitForm = function () {
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var loginName = document.getElementById("uname").value;
var myEmail = document.getElementById("email").value;
var pass1 = document.getElementById("password1").value;
var pass2 = document.getElementById("password2").value;
console.log(pass1, pass2);
if (document.getElementById("uname").value == "") {
alert("Your user name can't be blank.");
return false;
}

if (pass1 !== pass2) {
document.getElementById("signin").value.disabled = true;
alert("Please retype your password.");
return false;
} else if (pass1 === pass2) {
alert("Welcome!");
}
};
 <body>
<form action="" name="form" method="POST">
<label for="fname">First Name:</label><input type ="text" id = "fname" required></input>

<label for="lname">Last Name:</label><input type = "text" id = "lname" required></input>

<label for="uname">User Name:</label><input type ="text" id ="uname" required></input><br/>

<label for="password1">Password:</label><input type="password" id="password1" required></input><br/>

<label for="password2">Verify Password:</label><input type="password" id="password2" required ></input><br/>

<label for="email">Email Address:</label><input type="email" id="email" required></input><br/>

<button type="submit" id="signin" onclick="submitForm()">Submit</button>
</form>
</body>

关于javascript - 类型错误 : document. getElementById(...);在 JavaScript 中为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33795400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com