注册 登录
编程论坛 JavaScript论坛

获得焦点问题

tianbian210 发布于 2010-04-12 20:08, 824 次点击
程序代码:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312" contentType="text/html; charset=gb2312"%>
<html>
    <head>
        <title>
        </title>
        <script type="text/javascript">
        function checkdata(){
            if(form1.username.value==''){   
                alert("用户名不能为空");
                document.getElementById('username').focus();
                return false;
            }
            else if(document.form1.password.value==''||document.form1.swd.value==''){     
                alert("密码不能为空");
                document.getElementById('password').focus();
                return false;
            }
            else if(document.form1.password.value!=document.form1.swd.value)
            {
                alert("密码不一致");
                document.getElementById('password').focus();
                return false;
            }
            else{
                response.sendRedirect("LoginCl.jsp");
                return true;
            }
        }
       </script>
    </head>
    <body>
    <form name=form1 method="post">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td>确认密码: </td>
                <td><input type="password" name="swd"></td>
            </tr>
            <tr>
                <td><input type="reset" value="重置"><input type="submit" value="提交申请" onclick=checkdata()></td>
            </tr>
        </table>      
    </form>

 </body>
</html>
想实现在没有输入用户名的时候提示“用户名为空”,然后光标停在username输入框中
请问代码有什么问题
2 回复
#2
学飞的小鸟2010-04-12 22:00
你写的函数带返回值 但是你在调用的时候 函数前面没有用return  在提交按钮的单击事件应该是onclick="return checkdata()" 这么写 就对了

[ 本帖最后由 学飞的小鸟 于 2010-4-12 22:01 编辑 ]
#3
tianbian2102010-04-12 22:24
回复 2楼 学飞的小鸟
是的,谢谢了噢
1