0

在获取请求发送密码匹配之前,我有以下 html 表单使用 js 完成,但 js 无法正常工作

<html>
<title>Login System</title>
<head>
<script type="text/javascript">
function match()
{
var s1=document.getElementById('t4').value;
var s2=document.getElementById('t5').value;
if (s1 == s2)
{
alert('yep');
}
else
{
alert('Passwords Does not Match');
}
}
</script>
</head>
<style type="text/css">
div.ex {
width:220px;
padding:10px;
border:5px solid gray;
margin:10px;
}
</style>

<body>
<div class="ex">
<strong>User Login</strong>
<form name=f1 method=get action="http://localhost:8080/login">
User &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type=text name=t1><br>
Password <input type=password name=t2><br>
<input type=submit name=sub value="Login">
</form>
</div>
<div class="ex">
<strong>User Registration</strong>
<form name=f2 method=get action="http://localhost:8080/login">
User Name &nbsp&nbsp&nbsp&nbsp&nbsp<input type=text name=t3><br>
Password <input type=password name=t4><br>
Password <input type=password name=t5><br>
<input type=submit name=sub value="reg" onclick='match();'>
</form>
</div>

</body>
</html>
4

4 回答 4

3

getElementById查找id属性,而不是name您在上面使用的属性。

改变:

Password <input type=password name=t4><br>

Password <input type=password name=t4 id='t4'><br>
于 2013-02-25T05:05:35.967 回答
1

这是因为你刚刚给出了 thenname属性

Password <input type=password name=t4><br>
Password <input type=password name=t5><br>

做了

Password <input type=password name=t4 id=t4><br>
Password <input type=password name=t5 id=t5><br>

name同时给出和是一个好习惯id。帮助你,它不会失败:)
js 将采用id(作为您的代码)并且request对象将采用name

于 2013-02-25T05:08:43.743 回答
0

尝试这个:

<html>
<title>Login System</title>
<head>
<script type="text/javascript">
function match()
{
    var s1=document.getElementById('t4').value;
    var s2=document.getElementById('t5').value;

    if(s1!="" && s2!="")
    {
        if (!(s1 == s2))
        {
            alert('Passwords does not Match');
            return false;
        }
    }
    else
    {
        alert("Please enter password");
        return false;
    }
}
</script>
</head>
<style type="text/css">
div.ex {
    width:220px;
    padding:10px;
    border:5px solid gray;
    margin:10px;
}
</style>

<body>
    <div class="ex">
        <strong>User Login</strong>
        <form name=f1 method=get action="http://localhost:8080/login">
            User &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input type=text name=t1><br>
            Password <input type=password name=t2 /><br />
            <input type=submit name=sub value="Login" />
        </form>
    </div>
    <div class="ex">
        <strong>User Registration</strong>
        <form name=f2 method=get action="http://localhost:8080/login" onsubmit="return match();">
        User Name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=text name='t3' id='t3'><br>
        Password <input type=password name='t4' id='t4' /><br />
        Password <input type=password name='t5' id='t5' /><br />
        <input type=submit name=sub value="reg" />
</form>
</div>

</body>
</html>
于 2013-02-25T05:18:57.133 回答
0

您也可以通过不 chanidn 文本框仅更改以下代码来尝试此操作

var s1=document.getElementByName('t4');
var s2=document.getElementByName('t5');

这也将帮助您获得这些值。

于 2013-02-25T05:27:14.510 回答