2

我在下面有验证表单的代码。但是,此代码中有一个错误。当我在用户名中输入内容并将密码留空时,表单会接受它。它不应该接受它。知道为什么吗?

<html> 

     <head>  <link type = "text/css" type="text/javascript" rel="stylesheet" href="css/bootstrap.css" />
              <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
       <script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>

       <script src="js/bootstrap.js"> </script>
     <script> 
       $(document).ready(function() {
    $('form').validate({
        submitHandler: function(form) { // for demo
            alert('valid form');
            return false;
        }
    });
});</script>
     </head> 


 <body> 
<form class="well span6"> 

     <label>Username</label> 
     <input type="text" id="myName" class="span3 required" placeholder="Type your username here..." />  <br/> 

      <label>Password</label> 
     <input type="text" id="myPassword" class="span3 required" placeholder="Type your password here..." /> <br/> 

     <input type="submit" class ="btn btn-primary">
     <!-- <button class=""> Clear <br/></button><br/> -->

</form> 

</body> 

</html> 
4

2 回答 2

6

我很确定这个验证插件就像字段上的“名称”属性。

我在每个输入上都添加了name="myNamename="myPassword,它似乎在这个jsfiddle中工作

<form class="well span6">    
     <label>Username</label> 
     <input name="myName" type="text" id="myName" class="span3 required" placeholder="Type your username here..." /><br/> 

      <label>Password</label> 
     <input name="myPassword" type="text" id="myPassword" class="span3 required" placeholder="Type your password here..." /><br/> 

     <input type="submit" class="btn btn-primary">
</form> 
于 2013-01-02T16:27:10.920 回答
2

尝试通过将“名称”属性添加到密码和用户名字段。

于 2013-01-02T16:32:49.230 回答