0

我使用的是 1.9.0 jquery 版本,以及这个验证插件:

http://docs.jquery.com/Plugins/Validation

我想从 PHP 文件中下载 JSON 格式的消息,如下所示:

    messages: {
        remote: function() {
            return $.ajax({
                url: 'http://localhost/My/index.php/form/validate/messages',
                type: "POST",
                }).done(function(messages) {
                    return messages;
                }
            );
         }
    }

但是,它根本不起作用,不显示消息,进行 ajax 调用,什么都没有。你能帮我做这个吗?

4

2 回答 2

0

使用remote规则时,请参阅文档以获取示例:

http://docs.jquery.com/Plugins/Validation/Methods/remote#options

remote规则用于将特定字段的内容与您的数据库进行比较……例如检查名称或密码是否已经存在。它不能用于简单地将错误消息列表转储回插件。就插件而言,您只是为一条规则设置一条错误消息。见:http: //jsfiddle.net/zga8y/

我真的不明白你想要做什么的目的。通常,每个规则的错误消息设置一次并且不会更改。

动态更改特定字段上的消息,您可以使用rules('add')方法. 您甚至不必触碰规则即可覆盖消息。

$("#myinput").rules("add", {
    messages: {
        required: "Required input",
        minlength: jQuery.format("Please, at least {0} characters are necessary")
    }
});

或者,您可以覆盖任何或所有消息,如下所示:

jQuery.extend(jQuery.validator.messages, {
    required: "This field is required.",
    remote: "Please fix this field.",
    email: "Please enter a valid email address.",
    url: "Please enter a valid URL.",
    date: "Please enter a valid date.",
    dateISO: "Please enter a valid date (ISO).",
    number: "Please enter a valid number.",
    digits: "Please enter only digits.",
    creditcard: "Please enter a valid credit card number.",
    equalTo: "Please enter the same value again.",
    accept: "Please enter a value with a valid extension.",
    maxlength: jQuery.validator.format("Please enter no more than {0} characters."),
    minlength: jQuery.validator.format("Please enter at least {0} characters."),
    rangelength: jQuery.validator.format("Please enter a value between {0} and {1} characters long."),
    range: jQuery.validator.format("Please enter a value between {0} and {1}."),
    max: jQuery.validator.format("Please enter a value less than or equal to {0}."),
    min: jQuery.validator.format("Please enter a value greater than or equal to {0}.")
});
于 2013-02-28T23:39:10.580 回答
0

试试这样:

$.ajax({
        url: 'http://localhost/My/index.php/form/validate/messages',
        type: 'POST',
        dataType: 'json',
        success: function (results) {
            //your data here in results
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
         alert("error");
        }
    });

当显示“错误”时,ServerSide 出现问题。你需要调试它。您可以使用 Firebug 网络控制台检查结果或在浏览器中使用网络控制台。

于 2013-02-28T22:33:48.367 回答