-2

好的,所以我正在为登录类型的表单编写一个 javascript 代码,它将引导您进入一个新页面。这里是:

function submit1()
{
    var x=document.getElementById("username");
    var y=document.getElementById("password");
    if (x.value=="username" && y.value=="password")
    {
        window.location="Example.php";
    }
    else
    {
        window.alert=("The information you have submitted is incorrect and needs to be submitted again!");
    }
}

当我点击提交按钮时,它会将我直接带到页面而不是检查它是否正确。请帮忙!提前谢谢你!让您知道这不是永久登录页面!

4

4 回答 4

3

执行此操作的简单方法是使用button输入:

<input type="button" value="Check" onclick = "submit1();" />

submit另一种方法是通过使处理程序返回来防止类型输入的这种默认行为false。您的 HTML 将如下所示:

<input type="submit" value="Check" onclick = "return submit1();" />

您的功能需要更改好(考虑到您希望它重定向的事实)。我假设您想保留输入的数据,所以我不打算使用window.location重定向。相反,我将允许提交表单:

function submit1()
{
    var x=document.getElementById("username");
    var y=document.getElementById("password");
    if (x.value == "username" && y.value == "password") {
        window.alert=("The information you have submitted is incorrect and needs to be submitted again!");
        return false;
    }
}
于 2012-12-08T16:16:57.377 回答
2
<html>
<head>
<title>
Login page
</title>
</head>
<body>
<h1 style="font-family:Comic Sans Ms;text-align="center";font-size:20pt;
color:#00FF00;>
Simple Login Page
</h1>
<form name="login">
Username<input type="text" name="userid"/>
Password<input type="password" name="pswrd"/>
<input type="button" onclick="check(this.form)" value="Login"/>
<input type="reset" value="Cancel"/>
</form>
<script language="javascript">
function check(form)/*function to check userid & password*/
{
 /*the following code checkes whether the entered userid and password are matching*/
 if(form.userid.value == "myuserid" && form.pswrd.value == "mypswrd")
  {
     window.location="Example.php"; /*opens the target page while Id & password matches*/
  }
 else
 {
   alert("Error Password or Username")/*displays error message*/
  }
}
</script>
</body>
</html>
于 2012-12-08T16:21:38.240 回答
1

该事件需要取消默认事件并返回false。这将阻止表单提交。

但是,如果表单仍然提交,这应该不是问题,因为 JavaScript 不能被信任,因此您必须验证所有输入服务器端。

于 2012-12-08T16:23:51.373 回答
1
<form method="post" action="." id="myform">
 <!-- form contents --->
</form>    
<script type="text/javascript">
(function () {
    var f = document.getElementById('myform'), // get your form element
        x = document.getElementById('username'),
        y = document.getElementById('password'),
        handler;
    handler = function (e) {
        e.preventDefault(); // stop submit
        if (x.value=='username' && y.value=='password') {
            window.location = 'Example.php';
        } else {
            window.alert('The information...');
        }
    };
    // listen to submit event
    if ('addEventListener' in f) {
        f.addEventListener('submit', handler, false);
    } else { // handle also IE...
        f.attachEvent('submit', function () {
            handler(window.event);
        });
    }
}());
</script>

无论如何,看起来您正在尝试从 JS 检查登录名/密码,这不是最好的主意(任何人都可以查看源代码并阅读它)

于 2012-12-08T16:26:56.667 回答