JS代码不会从JSP调用

时间:2014-02-01 07:33:50

标签: javascript html

请考虑以下事项:

<!-- Client registration : the client hit Register on Main page -->

<%@ page language="java" 
    contentType="text/html; charset=windows-1256"
    pageEncoding="windows-1256"
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head><title>Client's Regsitration page</title>
<link rel="stylesheet"
      href="./css/styles.css"
      type="text/css"/>
</head>
<body>


<h1>Client's Regsitration page</h1>
<h1>
Let's register you to MyAmazon Online
</h1>

<!-- JS Code to make sure that the user MUST enter something in the login page -->
<script>
function verifyEmpty()
{
    var first = document.forms["reg"]["firstName"].value;
    var last = document.forms["reg"]["lastName"].value;
    var addr = document.forms["reg"]["address"].value;
    var idnum = document.forms["reg"]["idNumber"].value;
    var username = document.forms["reg"]["username"].value;
    var password = document.forms["reg"]["password"].value;

    alert("I'm in!");

    if (username == null || username == '' || password == null || password == '' ||
            first == null || first == '' || 
            last == null || last == '' ||
            addr == null || addr == '' ||
            idnum == null || idnum == '')
    {
        alert("All fields are required , please fill them all!");
        return false;
    }
    return true;
}     
</script>

<fieldset>
  <legend>Please fill in the followings :</legend>
  <form action="RegisterAmazon_after" onsubmit="return verifyEmpty(this)" id="reg">
  <p style="font-size:15px">  <span style="color:red;font-weight:bold;">*</span> <strong> First-name: </strong> <input type="text" name="firstName"><br> </p>
  <p style="font-size:15px">  <span style="color:red;font-weight:bold;">*</span> <strong> Last-name : </strong> <input type="text" name="lastName"><br> </p>
  <p style="font-size:15px">  <span style="color:red;font-weight:bold;">*</span> <strong> Address   : </strong> <input type="text" name="address"><br> </p>
  <p style="font-size:15px">  <span style="color:red;font-weight:bold;">*</span> <strong> ID-number : </strong> <input type="text" name="idNumber"><br> </p>
  <p style="font-size:15px">  <span style="color:red;font-weight:bold;">*</span> <strong> Username  : </strong> <input type="text" name="userName"><br> </p>
  <p style="font-size:15px">  <span style="color:red;font-weight:bold;">*</span> <strong> Password  : </strong> <input type="password" name="password"><br> </p> 
    <input type="submit" value="Register me!">
  </form>
</fieldset>

</body></html>

当我提交空白时,onsubmit标记不会被调用。警报I'm in也不起作用。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

一些错别字,只是解决了它:

表单必须以“reg”作为名称,而不是id:

<form action="RegisterAmazon_after" onSubmit="return verifyEmpty(this)" name="reg">
  <p style="font-size:15px">  <span style="color:red;font-weight:bold;">*</span> <strong> First-name: </strong> <input type="text" name="firstName"><br>  
  <p style="font-size:15px">  <span style="color:red;font-weight:bold;">*</span> <strong> Last-name : </strong> <input type="text" name="lastName"><br>  
  <p style="font-size:15px">  <span style="color:red;font-weight:bold;">*</span> <strong> Address   : </strong> <input type="text" name="address"><br>  
  <p style="font-size:15px">  <span style="color:red;font-weight:bold;">*</span> <strong> ID-number : </strong> <input type="text" name="idNumber"><br>  
  <p style="font-size:15px">  <span style="color:red;font-weight:bold;">*</span> <strong> Username  : </strong> <input type="text" name="userName"><br> 
  <p style="font-size:15px">  <span style="color:red;font-weight:bold;">*</span> <strong> Password  : </strong> <input type="password" name="password"><br>  
      <input type="submit" value="Register me!" name="submit" />
  </form>

和(userName而不是用户名)

function verifyEmpty()
{
    var first = document.forms["reg"]["firstName"].value;
    var last = document.forms["reg"]["lastName"].value;
    var addr = document.forms["reg"]["address"].value;
    var idnum = document.forms["reg"]["idNumber"].value;
    var username = document.forms["reg"]["userName"].value;
    var password = document.forms["reg"]["password"].value;

    alert("I'm in!");

    if (username == null || username == '' || password == null || password == '' ||
            first == null || first == '' || 
            last == null || last == '' ||
            addr == null || addr == '' ||
            idnum == null || idnum == '')
    {
        alert("All fields are required , please fill them all!");
        return false;
    }
    return true;
}  

答案 1 :(得分:1)

JSfiddle-&gt;回答你的问题

http://jsfiddle.net/kDLWA/9/

username需要更改为userName,您不需要使用this

希望有所帮助