在SO上经历了其他类似的问题,但无法解决我的问题。我src在script标签中写了路径。
src
script
下面是程序:
function validate() { var fname = document.myForm.fname.value; var lname = document.myForm.lname.value; if(fname=="" && lname=="") { alert("First and last name cannot be emtpy"); return false; } } <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="/js/validation.js"></script> <title>Registration Form</title> </head> <body> <form name="myForm" action="RegistrationServlet.do" method="POST" onsubmit="return validate()"> First name:<input type="text" name="fname" /> <br/> Last name:<input type="text" name="lname" /><br/> Gender: Male<input type="radio" name="male" /> Female<input type="radio" name="male" /><br/> Mobile:<input type="tel" name="mobile" /><br/> Email:<input type="email" name="email" /><br/> Address:<textarea name="tarea"></textarea><br/> Street:<input type="text" name="street" /><br/> Area:<input type="text" name="area" /><br/> City:<select> <option value="hyderabad">hyderabad</option> <option value="secunderabad">secunderabad</option> <option value="delhi">delhi</option> <option value="mumbai">mumbai</option> </select> State: <input type="text" name="state" /><br/> Country: <input type="text" name="country" /><br/> Pin: <input type="text" name="pin" /><br/> <input type="submit" value="Submit" /> </form> </body> </html>
我的js文件位于webapp目录内名为js的文件夹中
下面的行解决了我的问题:
<script src="<%request.getContextPath()%>/js/validation.js"></script>