JavaScript 登录表单 document.getElementById 未检测到值
JavaScript Login form document.getElementById is not detecting value
谁能告诉我为什么 document.getElementById 没有检测到在下面的代码中输入的密码:
function myFunction() {
let email = "name@email.com";
let password = "password";
if (document.getElementById("password") == password) {
Console.console.log("success");
} else {
console.log("Failure");
console.log(password);
console.log(document.getElementById("password"));
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form action="#">
<label for="Email">Email</label>
<input id="email" type="email">
<label for="password">Password</label>
<input id="password" type="text">
<button onclick="myFunction()">Submit</button>
</form>
</body>
</html>
当我尝试在控制台中记录它的值时,我只得到输入 id="password" type="text" 我不确定这意味着什么,除了出于某种原因它没有我想要分配给它的值。
-谢谢
函数 document.getElementById
returns 一个 DOM 元素对象。此对象具有各种属性,如 .style
,但要获取为 <input>
元素输入的文本,您需要 .value
属性。
function myFunction() {
let email = "name@email.com";
let password = "password";
if (document.getElementById("password").value == password){
console.log("success");
} else {
console.log("Failure");
console.log(password);
console.log(document.getElementById("password").value);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form action="#">
<label for="Email">Email</label>
<input id="email" type="email">
<label for="password">Password</label>
<input id="password" type="text">
<button onclick="myFunction()">Submit</button>
</form>
</body>
</html>
更改这些行
if(document.getElementById("password") == password){
console.log(document.getElementById("password"));
这些行
if(document.getElementById("password").value == password){
console.log(document.getElementById("password").value);
鲍勃是你的叔叔。
你的实现没问题,但是你犯了一个小错误。 document returns 是一个有多个键的对象。在这些键中 'value' 键包含您输入字段的值。
document.getElementById("password").value
将 return 您想要的值。
注意:您可以从此对象访问所有属性。例如
document.getElementById("password").placeholder
将return来自输入字段的提示
谁能告诉我为什么 document.getElementById 没有检测到在下面的代码中输入的密码:
function myFunction() {
let email = "name@email.com";
let password = "password";
if (document.getElementById("password") == password) {
Console.console.log("success");
} else {
console.log("Failure");
console.log(password);
console.log(document.getElementById("password"));
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form action="#">
<label for="Email">Email</label>
<input id="email" type="email">
<label for="password">Password</label>
<input id="password" type="text">
<button onclick="myFunction()">Submit</button>
</form>
</body>
</html>
当我尝试在控制台中记录它的值时,我只得到输入 id="password" type="text" 我不确定这意味着什么,除了出于某种原因它没有我想要分配给它的值。
-谢谢
函数 document.getElementById
returns 一个 DOM 元素对象。此对象具有各种属性,如 .style
,但要获取为 <input>
元素输入的文本,您需要 .value
属性。
function myFunction() {
let email = "name@email.com";
let password = "password";
if (document.getElementById("password").value == password){
console.log("success");
} else {
console.log("Failure");
console.log(password);
console.log(document.getElementById("password").value);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form action="#">
<label for="Email">Email</label>
<input id="email" type="email">
<label for="password">Password</label>
<input id="password" type="text">
<button onclick="myFunction()">Submit</button>
</form>
</body>
</html>
更改这些行
if(document.getElementById("password") == password){
console.log(document.getElementById("password"));
这些行
if(document.getElementById("password").value == password){
console.log(document.getElementById("password").value);
鲍勃是你的叔叔。
你的实现没问题,但是你犯了一个小错误。 document returns 是一个有多个键的对象。在这些键中 'value' 键包含您输入字段的值。
document.getElementById("password").value
将 return 您想要的值。
注意:您可以从此对象访问所有属性。例如
document.getElementById("password").placeholder
将return来自输入字段的提示