jQuery .validate()变量错误消息 [英] jquery .validate() variable error message

查看:66
本文介绍了jQuery .validate()变量错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此网站上使用多种语言,并希望以不同的语言显示错误.我想知道是否可以在自定义错误消息中使用变量.

这是JavaScript代码的片段:

  $('form').validate({$ .ajax({网址:"notification.php",dataType:'json',成功:功能(数据){返回通知=数据;}});规则:{数量:{必填:是,数字:true}},讯息:{数量:{必填:notification ['qty_error'],位数:通知['qty_error2']}},invalidHandler:function(){警报(notification ['error2']);},SubmitHandler:函数(形式){$ .ajax({网址:$('form').attr('action'),类型:"post",数据:$('form').serialize(),成功:function(){$ .ajax({网址:"notification.php",dataType:'json',成功:功能(数据){$('#notification .container').html('< div class ="success" style ="display:none;">'+ data.confirm1 +'< img src ="catalog/view/theme/default/image/close.png"alt =""class =" close"/></div>');$('#notification .success').fadeIn('slow');}});}});}}); 

如果不是,还有另一种方法.

解决方案

您无法像在此处那样将 .ajax()放在 .validate()内...

  $('form').validate({$ .ajax({...});//<-删除此...}); 

只有 jQuery Validate插件文档中指定的规则,选项和回调函数才能进入

.validate().

我看到您已经在 submitHandler 回调函数中包含了相同的 .ajax()代码.这是完全正确的,因此只需按照上述指示删除 .ajax().

  $('form').validate({//规则,选项和回调函数...SubmitHandler:函数(形式){$ .ajax({...});//<-ajax始终属于此处返回false;}}); 


对于最初的问题,,您可以使用变量代替消息.

  $(document).ready(function(){var message_1 =我的自定义消息1";$('#myform').validate({规则:{栏位1:{必填:true}},讯息:{栏位1:{必填:message_1}}});}); 

演示: http://jsfiddle.net/PWdke/

要动态更改消息(在初始化插件之后),需要 .rules('add')方法.

  $(document).ready(function(){var message_1 =我的自定义消息1",message_3 =我的自定义消息3";$('#myform').validate({规则:{栏位1:{必填:true}},讯息:{栏位1:{必填:message_1}}});//以编程方式覆盖下面的初始化规则/消息$('input [name ="field1"]').rules('add',{必填:是,讯息:{必填:message_3}});}); 

演示: http://jsfiddle.net/PWdke/1/

I am using multiple languages on this website and want to display the errors in the different languages. I was wondering if it is possible to use variables in the custom error messages.

Here is the snippet of the JavaScript code:

$('form').validate({
$.ajax({
    url: 'notification.php',
    dataType: 'json',
    success: function(data) {
        return notification = data;
    }
});
rules: {
    qty: {
        required:   true,
        digits:     true
    }
},
messages: {
    qty: {
        required:   notification['qty_error'],
        digits:     notification['qty_error2']
    }
},
invalidHandler: function () {
    alert(notification['error2']);
},
submitHandler: function(form) {
    $.ajax({
        url:    $('form').attr('action'),
        type:   'post',
        data: $('form').serialize(),
        success: function() {
            $.ajax({
                url: 'notification.php',
                dataType:'json',
                success: function(data) {
                    $('#notification .container').html('<div class="success" style="display: none;">'+ data.confirm1 + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
                    $('#notification .success').fadeIn('slow');
                }
            });

        }
    });
}
});

If not what is another way of doing it.

解决方案

You cannot place .ajax() inside of .validate() like you've done here...

$('form').validate({
    $.ajax({ ... });  // <-- remove this
    ...
});

Only the designated rules, options, and callback functions as per the jQuery Validate plugin documentation can ever go inside of .validate().

I see you've already included the same .ajax() code inside the submitHandler callback function. This is perfectly correct, so just remove the .ajax() as indicated above.

$('form').validate({
    // rules, options & callback functions
    ...
    submitHandler: function(form) {
        $.ajax({ ... });  // <-- ajax always belongs here
        return false;
    }
});


As far as your original question, yes, you can use variables in place of the messages.

$(document).ready(function() {

    var message_1 = "my custom message 1";

    $('#myform').validate({
        rules: {
            field1: {
                required: true
            }
        },
        messages: {
            field1: {
                required: message_1
            }
        }
    });

});

DEMO: http://jsfiddle.net/PWdke/

To change the messages dynamically (after the plugin is initialized) would require the .rules('add') method.

$(document).ready(function() {

    var message_1 = "my custom message 1",
        message_3 = "my custom message 3";

    $('#myform').validate({
        rules: {
            field1: {
                required: true
            }
        },
        messages: {
            field1: {
                required: message_1
            }
        }
    });

    // programatically over-ride initialized rules/messages below

    $('input[name="field1"]').rules('add', {
        required: true,
        messages: {
            required: message_3
        }
    });

});

DEMO: http://jsfiddle.net/PWdke/1/

这篇关于jQuery .validate()变量错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆