我的代码有什么问题不是调用函数JSP

时间:2015-08-27 16:42:57

标签: javascript function jsp

如果我离开一个空的字段但我没有工作,我正试图获得警报,它只是在不调用该函数的情况下进入下一步。我忘了把" onsubmit ="返回验证()"

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
            <script type =text/javascript>
                function validation() {

                        var errormessage = "";

                    if (document.getElementById('firstName').value == "") {
                        errormessage += "please enter the firstname \n";
                        document.getElementById("firstName").style.borderColor =            "red";

                    }

    if (errormessage != "") {

                    alert(errormessage);
                    return false;

                }

            }
        </script>
</head>

<body>
    <form action ="anotherpage.jsp" method="post" autocomplete="on"      onsubmit="return validation()>
                  Firstname:<input type="text" id="firstName" name="name" value= "            <%=name%>"  />
              <br>



         <input type="submit" value="Next Step" name="nextStep" />
         </form>

    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

请在脚本标记

中添加引号
<script type="text/javascript">

嘿,你没有调用验证功能

在表单中替换

Firstname:<input type="text" id="firstName" name="name" value= "<%=name%>"  />

用这个并再次测试

  Firstname:<input type="text" id="firstName" name="name" onsubmit="return validation();" value= "<%=name%>"/>