处理程序不适用于JavaScript

时间:2014-01-12 16:42:30

标签: javascript event-handling onsubmit

请参阅以下代码:

<html>
    <head>
        <script type="text/javascript">
            function validate()
            {
                fname = f.fn.value;
                if(fname!= "abc")
                    alert("Incorrect name!")

                lname = f.ln.value;
                if(lname != "xyz")
                    alert("Incorrect Name!")

                paswd = f.pswd.value;
                if(paswd<8)
                    alert("Too short password!")

                for(var i=0; i<f.d.length; i++)
                {
                    if(f.d[i].value.checked)
                    {
                        document.write(f.d[i].value);
                    }   
                }

                for(var i=0; i<f.c.length; i++)
                {
                    if(f.c[i].value.checked)
                    {
                        alert(f.c[i].value);
                    }
                }
            }
        </script>
    </head>

    <body>
        <form name="f" onsubmit="validate()">
            First Name: <input type = "text" name = "fn"> <br>
            Last Name: <input type = "text" name = "ln"> <br>
            Password: <input type = "password" name = "pswd"> <br>
            E-mail: <input type = "email" name = "mail"> <br>
            Degree : <input type = "radio" name = "d" value = 's'> SE 
            <input type = "radio" name = "d" value = 'c'>CS 
            <input type = "radio" name = "d" value = 'E'>IT <br>
            University
            <select name = "uni">
                <option value = 'p'>PU</option>
                <option value = 'f'>FAST</option> 
            </select> <br>
            CGPA : <input type = "radio" name = "c" value = '3'> >=3.0
            <input type = "radio" name = "c" value = '2'> >=2.5 <br>
            Browse <input type = "file" name = "uf">  <br>
            <input type = "Submit" value = "Submit">
        </form>
    </body>
</html>

当我按下提交按钮时,我应该收到

的消息
  

名称不正确

  

密码太短

如果条件成立但没有任何反应,为什么? 为什么

  

验证()

功能没有运行?

3 个答案:

答案 0 :(得分:1)

问题

int i

毫无意义。这个 Java 语法,但不是 Javascript。我认为你的意思是var i

还有什么?

您有两个表单标记。

PS

如果您懒得打开网络浏览器的控制台(或者没有控制台),只需使用trycatch表达式。

我懒得解决所有这些问题。请给我一些固定的代码

<强> DEMO

答案 1 :(得分:1)

首先:

只需使用一次表单:

<form> // <--- remove this line
<form name="f" onsubmit="validate()">

其次,你正在使用看起来像JAVA和JavaScript的混合物,所以代替for(int i,用var声明你的变量。像这样:

for (var i = 0; i < f.d.length; i++) { <--- var instead of int
    if (f.d[i].value.checked) {
        alert(f.d[i].value);
    }
}

这应该删除所有错误,您可能在使用正确的调试工具时自己也看到了这些错误。以下是它们的列表:

答案 2 :(得分:1)

代码中出现错误

 <html>
  <head>
   <script type="text/javascript">
function validate()
{
  fname = document.f.fn.value;
  if(fname!= "abc")
  alert("Incorrect name!");

  lname = f.ln.value;
  if(lname != "xyz");
  alert("Incorrect Name!");
  paswd = f.pswd.value;
  if(paswd<8)
  alert("Too short password!")
   for(i=0; i<f.d.length; i++)
    {
    if(f.d[i].value.checked)
    {
        document.write(f.d[i].value);
    }   
     }
    for(i=0; i<f.c.length; i++)
     {
    if(f.c[i].value.checked)
    {
        alert(f.c[i].value);
    }
      }
   // return false;  // use this you don't want to submit form
   // return true;  //for advancing the form
   }
 </script>
</head>

<body>

<form action="" name="f" onsubmit=" return validate()">
First Name: <input type = "text" name = "fn"> <br>
Last Name: <input type = "text" name = "ln"> <br>
Password: <input type = "password" name = "pswd"> <br>
E-mail: <input type = "email" name = "mail"> <br>
Degree : <input type = "radio" name = "d" value = 's'> SE 
<input type = "radio" name = "d" value = 'c'>CS 
<input type = "radio" name = "d" value = 'E'>IT <br>
University <select name = "uni">
<option value = 'p'>PU</option>
<option value = 'f'>FAST</option> 
</select> <br>
CGPA : <input type = "radio" name = "c" value = '3'> >=3.0
<input type = "radio" name = "c" value = '2'> >=2.5 <br>
Browse <input type = "file" name = "uf">  <br>
<input type = "submit" value = "Submit">
</form>
</body>
</html>

上面的代码正在运行

我认为您正在对此表单执行验证,因此您需要调用函数

return validate();

现在,如果函数返回false,则表单未提交

如果函数返回true,则提交表单

请求进一步的帮助,不要浪费我的努力