-1

showErrors在 jquery 表单验证中使用在表单顶部显示错误消息。一次我想显示一条错误消息。在我的表格name,email,url and comment中有四个可用的字段。如果用户直接单击提交按钮而不输入任何数据,则显示name字段错误,但当用户关注字段时,email它会覆盖两者的错误消息。nameemail

jQuery :-

$(document).ready(function(){
    $("form").validate({
    messages: {
         name: "Please specify your name.",
         email: {
           required: "We need your email address to contact you.",
           email: "Your email address must be in the format of name@domain.com."
         },
         url: "A valid URL, please.",
         comment: "Please enter your comment."
       },
    showErrors: function(errorMap, errorList) {
            if(errorList.length) {
                $("span").html(errorList[0].message);
            }
        }
    });
});

这是小提琴:- http://jsfiddle.net/roop1886/q64VE/

4

1 回答 1

0

解决方案:- 我 focusInvalid: false,在功能中添加了@Barmar 的建议$("form").validate()

$(document).ready(function(){
    $("form").validate({
   focusInvalid: false,
    messages: {
         name: "Please specify your name.",
         email: {
           required: "We need your email address to contact you.",
           email: "Your email address must be in the format of name@domain.com."
         },
         url: "A valid URL, please.",
         comment: "Please enter your comment."
       },
    showErrors: function(errorMap, errorList) {
            if(errorList.length) {
                $("span").html(errorList[0].message);
            }
        }
    });
});
于 2013-06-12T06:14:39.617 回答