[英]Can't figure out what is wrong with my JavaScript code
这是HTML代码:
<form action="">
<div data-role="fieldcontain">
<label for="username">
Username
</label>
<input class="usernamevalidation validate" name="username" id="username" placeholder="" value="" type="text" maxlength="20" required>
</div>
<div data-role="fieldcontain">
<label for="password">
Password
</label>
<input class="validate" name="password" id="password" placeholder="" value="" type="password" maxlength="20" required>
</div>
<input id="submitLogin" type="submit" value="Log In" data-transition="slide">
<div id="errormsg"></div>
</form>
这是JavaScript代码。 当用户名和密码为“ abc”时,我试图对要登录的页面进行硬编码。
username_test = document.getElementById("username").getAttribute("value") === "abc";
password_test = document.getElementById("password").getAttribute("value") === "abc";
if_true = window.location.href("Main_Menu.html");
if_false = document.getElementById("errormsg").innerHTML("<p style="color=red;">Invalid credentials.</p>");
function login() {
if (username_test && password_test)
{
if_true
}
else
{
if_false
}
}
尝试这个:
username_test = document.getElementById("username").getAttribute("value") === "abc";
password_test = document.getElementById("password").getAttribute("value") === "abc";
function login() {
if (username_test && password_test)
{
window.location.href = "Main_Menu.html";
}
else
{
document.getElementById("errormsg").innerHTML("<p style="color=red;">Invalid credentials.</p>");
}
}
要么
username_test = document.getElementById("username").getAttribute("value") === "abc";
password_test = document.getElementById("password").getAttribute("value") === "abc";
if_true = function() { window.location.href = "Main_Menu.html"; };
if_false = function () { document.getElementById("errormsg").innerHTML("<p style="color=red;">Invalid credentials.</p>"); };
function login() {
if (username_test && password_test)
{
if_true();
}
else
{
if_false();
}
}
您的代码中有两个错误。
首先 :
if_true = window.location.href("Main_Menu.html");
window.location.href
不是函数 。 将其放在if
语句中,因为它会立即执行。 您不能将其存储在某些变量中。 我的意思是可以,但仍会在调用它的那一行中执行。
第二 :
if_false = document.getElementById("errormsg").innerHTML("<p style="color=red;">Invalid credentials.</p>");
innerHTML
也不是一个函数 。 这是#errormsg
元素的属性 。 而且,您不能仅将函数结果绑定到变量并将其输出到其他期望在此调用的地方。
您的代码应如下所示:
username_test = document.getElementById("username").getAttribute("value") === "abc";
password_test = document.getElementById("password").getAttribute("value") === "abc";
function login() {
if (username_test && password_test)
{
window.location.href = "Main_Menu.html";
}
else
{
document.getElementById("errormsg").innerHTML = "<p style="color=red;">Invalid credentials.</p>";
}
}
我在这里看到您的代码有很多问题
login
分配给任何按钮操作。 你可以这样做 -
<form action="">
<div data-role="fieldcontain">
<label for="username">
Username
</label>
<input class="usernamevalidation validate" name="username" id="username" placeholder="" value="" type="text" maxlength="20" required>
</div>
<div data-role="fieldcontain">
<label for="password">
Password
</label>
<input class="validate" name="password" id="password" placeholder="" value="" type="password" maxlength="20" required>
</div>
<input id="submitLogin" onclick="login()" value="Log In" data-transition="slide">
<div id="errormsg"></div>
</form>
JS:
function login() {
username_test = document.getElementById("username").getAttribute("value") === "abc";
password_test = document.getElementById("password").getAttribute("value") === "abc";
if (username_test && password_test)
{
window.location.href = "Main_Menu.html";
}
else
{
document.getElementById("errormsg").innerHTML("<p style="color=red;">Invalid credentials.</p>");
}
}
尝试这个:
<form action="">
<div data-role="fieldcontain">
<label for="username">
Username
</label>
<input class="usernamevalidation validate" name="username" id="username" placeholder="" value="" type="text" maxlength="20" required>
</div>
<div data-role="fieldcontain">
<label for="password">
Password
</label>
<input class="validate" name="password" id="password" placeholder="" value=""type="password" maxlength="20" required>
</div>
<input id="submitLogin" type="submit" value="Log In" data-transition="slide" onclick="nextpage()">
<div id="errormsg"></div>
</form>
This is script code:
function nextpage()
{
username_test = document.getElementById("username").value
password_test = document.getElementById("password").value
if((username_test=="abc")&&(password_test=="abc"))
window.location.href("Main_Menu.html");
else
document.getElementById("errormsg").innerHTML="Invalid credentials.";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.