Jquery表单验证器:意外的标识符

时间:2013-06-21 05:38:36

标签: jquery

我正在使用基本的jquery表单验证器进行学习。每次我在浏览器中运行该页面时,都会收到Unexpected Identifier错误。我已经多次查看代码,看看它是否是报价问题,但没有运气。我该如何摆脱这个错误? LIVE DEMO

的script.js

$(document).ready(function(){
    $("#myform").submit(function() {
        var abort = false;
        $("div.error").remove();
        $(":input[required]").each(function(){
           if ($(this).val() === ""){
               $(this).after("<div class=\"error\"> This is a required field </div>");
               abort = true;
           }  
        });
        if (abort){return false;} else {
            postData = $("#myform").serialize();
            $.post("process.php", postData+"&action=submit&ajaxrequest=1", function(msg){
                if(msg){
                    $("#myform").before(msg);
                }
            });
            return false;
        }
    });
});

$("input[placeholder]").blur(function(){
   $("div.error").remove();
   var myPattern = $(this).attr("pattern");
   var myPlaceholder = $(this).attr("placeholder");
   var isValid = $(this).val().search(myPattern) >= 0;

   if (!isValid){
       $(this).focus();
       $(this).after("<div class=\"error\">The entered data does not match expected pattern: " + myPlaceholder + "</div>");
   } //isValid test
}); // onblur

enter image description here

2 个答案:

答案 0 :(得分:0)

应该用var声明

postData ,但这不是问题。你几乎肯定错误地放置了{}或;

我认为模糊函数也在$(document).ready(function() { block?内部,因为它可能的jQuery在此之前无法运行

答案 1 :(得分:0)

您可以尝试将所有代码放入文档就绪功能

$(document).ready(function(){
   $("#myform").submit(function() {
       //......all the code stuff here
   });

   $("input[placeholder]").blur(function(){
       //......all the code stuff here
   });
}); // end of doc ready