脚本片断如下:function checkinput()
{
if(""==document.loginform.sname.value){
window.alert("请输入学生姓名!");
document.loginform.name.focus();
return false;
}
 if(""==document.loginform.birth.value){
window.alert("请输入出生日期!");
document.loginform.birth.focus();
return false;
}
if(""==document.loginform.score.value){
window.alert("请输入分数!");
document.loginform.score.focus();
return false;
}
preprocess(document.loginform);
return true;
}网页代码如下:<form name="loginform" onSubmit="return checkinput();"
action="/javawebservice2(sms-client)/AddStuServlet" method="post"><input type="submit" value="增加学生"></form>
存在问题:
在几个text框中不写内容的时候虽然会弹出提示,但是总是会提交导致出错,在网上找了半天也没个头绪.烦请各位大侠给看看.谢谢了.在线等!~~~~~~~

解决方案 »

  1.   

    preprocess(document.loginform);什么意思?
      

  2.   

    preprocess(document.loginform);去掉呀
      

  3.   

    去掉了也不管用.....还是会提交~~~晕死....我把源码全部发过来.有兴趣的看看
    <%@ page language="java" pageEncoding="utf-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>增加学生!!~~</title> <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0"> <script type="text/javascript">
    function checkinput()
    {
    if(""==document.loginform.sname.value){
    window.alert("请输入学生姓名!");
    document.loginform.name.focus();
    return false;
    }
     if(""==document.loginform.birth.value){
    window.alert("请输入出生日期!");
    document.loginform.birth.focus();
    return false;
    }
    if(""==document.loginform.score.value){
    window.alert("请输入分数!");
    document.loginform.score.focus();
    return false;
    }

    return true;
    }
    function start()
    {
    document.loginform.sname.focus();
    }
    //在用户名处获得焦点;
    window.onload = start;

    </script>
    </head> <body>
    <br>
    <a href="/javawebservice2(sms-client)/Add.jsp">增加学生</a>&nbsp;
    <a href="/javawebservice2(sms-client)/Search.jsp">查询学生</a>
    <br>
    <br>
    <form name="loginform" onSubmit="return checkinput();"
    action="/javawebservice2(sms-client)/AddStuServlet" method="post">
    <table align="center" border="0" cellpadding="0" cellspacing="0"> <tr>
    <td>
    <br>
    学生姓名:
    </td>
    <td>
    <br>
    <input type="text" name="sname">
    </td>
    </tr>
    <tr>
    <td>
    <br>
    学&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;科:
    </td>
    <td>
    <br>
    <!-- <input type="text" name=""> -->
    <select name="major"> <option value="Chinese" selected>
    Chinese
    </option> <option value="English">
    English
    </option> <option value="Computer">
    Computer
    </option> </select> </td>
    </tr>
    <tr>
    <td>
    <br>
    出生日期:
    </td>
    <td>
    <br>
    <input type="text" name="birth">
    </td>
    </tr>
    <tr>
    <td>
    <br>
    分&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数:
    </td>
    <td>
    <br>
    <input type="text" name="score">
    </td>
    </tr>
    <tr align="center">
    <td colspan="2">
    <br>
    <br>
    <input type="submit" value="增加学生">
    &nbsp;&nbsp;&nbsp;&nbsp;
    <input type="reset" value="重新填写">
    </td>
    </tr>
    </table>
    </form> </body>
    </html>
      

  4.   

       if(""==document.loginform.sname.value){
            window.alert("请输入学生姓名!");
            document.loginform.name.focus();
            return false;
        }
    学生姓名的text的name到底是什么?
    你这里设置焦点的时候为name,判断的时候是sname!
      

  5.   

    重点的部分就是这里了...
    function checkinput()
    {    
        if(""==document.loginform.sname.value){
            window.alert("请输入学生姓名!");
            document.loginform.name.focus();
            return false;
        }
     if(""==document.loginform.birth.value){
            window.alert("请输入出生日期!");
            document.loginform.birth.focus();
            return false;
        }
    if(""==document.loginform.score.value){
            window.alert("请输入分数!");
            document.loginform.score.focus();
            return false;
        }
        
        return true;
    }
    还有这里.
     <form name="loginform" onSubmit="return checkinput();"
                action="/javawebservice2(sms-client)/AddStuServlet" method="post">
                <table align="center" border="0" cellpadding="0" cellspacing="0">
      

  6.   

    document.loginform.name.focus(); 
    这里name改成sname试试
      

  7.   

    function checkinput()
    {
       if(""==document.loginform.sname.value){
            window.alert("请输入学生姓名!");
            document.loginform.name.focus();
        }
        else
        {
            document.action="/javawebservice2(sms-client)/AddStuServlet";
            document.submit();
        }
    }
    试下这种方法看行不行,同时把form里的action去掉