javascript - 如何检查 jquery .validate() 中没有错误

标签 javascript jquery html jquery-validate

我在创建代码时遇到了一些问题,只有在所有文本字段都正确验证时才应该显示一个按钮。我在页面打开时隐藏按钮(我知道我可以使用 css 来执行此操作,但我只是想了解功能)并且我想在字段正确时显示按钮。我正在尝试使用成功选项,但它只是在填写任何内容之前立即显示按钮。有什么建议么?这是代码:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>  
<script type="text/javascript" src="js\jquery.validate.js"></script>
<script>
$(document).ready(function(){
var submit = false;
$(".btn").hide();
 $("#submission").validate({
   rules: {
      appSize: { 
         required:true,
         max:12 },
      appName: { 
         required:true,
         rangelength:[4,9] },
      appPrice: { 
         required:true,
         min:.99,
         max:3.99 },
      email: { 
         required:true,
         email:true }
   },

   messages: { 
      appSize: {
      required: "App Size is a required field"},
      appName: { 
         required: "App Name is a required field",
         rangelength: "The name must be between 4 and 9 characters" },
      appPrice: { 
         required: "App Price is a required field",
         min: "Price must be .99 or greater",
         max: "Price must be less than 3.99" },
      email: { 
         required: "email is a required field",
         email: "You must enter a valid email address" },
      success: submit = true

   },
   errorElement: "div",




 });
 if(submit)
    $(".btn").show();
 $(".btn").click(function() {

     alert("Data is Valid, Thanks for your submission");    
   });
});

</script>
</head>
<body>
<form id="submission" method="post" action="#">
  <h1>Apple iPhone App Contest Submission Page</h1>
    <div id="theForm">
        <div>
                 <label for="appSize">What is the file size in KB?</label>
                 <input name="appSize" type="text" id="fileSize" class="required error"></input>
        </div>
        <br />
        <div>
                 <label for="appName">What is the App Name?</label>
                 <input name="appName" type="text" id="appName" class="required error"></input>
        </div>
        <br />
         <div>
                 <label for="appPrice">What is the App Price?  $</label>
                 <input name="appPrice" type="text" id="appPrice" class="required error"></input>
        </div>
        <br />
        <div>
                 <label for="email">Submitters Email Address</label>
                 <input name="email" type="text" id="email" class="required error"></input>
        </div>
        <br /> 
       <input type="button" class="btn" value="Submit"></input>
     </div>
</form>
</body>
</html>

最佳答案

success: submit = true

这不会像您预期的那样工作。

为该属性分配一个函数并在其中执行您需要执行的操作。

关于javascript - 如何检查 jquery .validate() 中没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8379308/

相关文章:

javascript - 将 bool 值分配给对象内的键

javascript - HTML5 FileReader.readAsDataURL 的输出可以用于引用字体吗?

javascript - jQuery 模板系统用花括号中断

c# - 用于显示日期事件的日历控件

javascript - 从 JSON 文件中读取带有 html 标签的内容并在 javascript 中显示

javascript - 如何操作浏览器历史记录并捕获浏览器后退/前进按钮?

javascript - 使用 AES 算法在 javascript 中加密并在 C# 中解密

javascript - 具有多个 x 轴和 y 轴的 Highcharts 绘图线

javascript - 如何将 jQuery .live() 附加到这段代码?

jquery根据图像大小设置div容器的高度和宽度