简体   繁体   English

如何在jquery.validation中进行多个突出显示?

[英]How do I make multiple highlights in jquery.validation?

I am using jQuery.validator. 我正在使用jQuery.validator。 I finally found an really good answer to adding localstorage capability to jQuery.validator. 我终于找到了向jQuery.validator添加本地存储功能的一个很好的答案。

Is it possible to write jquery.validate error codes into localstorage? 是否可以将jquery.validate错误代码写入localstorage?

I using this code now, but I want to use now multiple highlight for more fields at the same time. 我现在使用此代码,但是现在我想同时使用多个高亮显示更多字段。 How do I make more instance of highlight and unhighlight? 如何制作更多高光和反高光的实例?

I change the name for example I used "highlight2". 我更改了名称,例如我使用了“ highlight2”。 This not work. 这行不通。

I am using same code as Brahim. 我正在使用与Brahim相同的代码。

highlight: function(element, errorClass, validClass) {
    // Check for your email input
        if (element.type === "email" && element.name === "input_name") {
        // `errorMap` is an internal Key/value store, where the key refers to the
        // name of an input field, values the message to be displayed for that
        // input.
        localStorage.setItem("email_error", this.errorMap[element.name]);
        }

        // Call the default highlight to keep the original behaviour
        $.validator.defaults.highlight(element, errorClass, validClass);
    },
      unhighlight: function(element, errorClass, validClass) {
        // Check for your email input
        if (element.type === "email" && element.name === "input_name") {
          localStorage.removeItem("email_error");
        }

I want more than one field to show error in localstorage. 我希望多个字段显示本地存储错误。 Sorry for my english. 对不起我的英语不好。

Unfortunately as of right now jquery.validation does not support multiple independent highlight and unhighlight functions. 不幸的是,到目前为止,jquery.validation不支持多个独立的突出显示和取消突出显示功能。 You can however use a validation script that currently has the capability. 但是,您可以使用当前具有该功能的验证脚本。 If you select parsley http://parsleyjs.org/ you can create multiple events utilizing their 'field:error' and 'field:success' functions. 如果您选择parsley http://parsleyjs.org/ ,则可以使用它们的'field:error'和'field:success'函数创建多个事件。 You can do something like; 你可以做类似的事情;

    $('#id_of_selector_1').parsley().on('field:error', function() {          
        localStorage.setItem('error_1', JSON.stringify(true));
    });
    $('#id_of_selector_1').parsley().on('field:success', function() {          
        localStorage.removeItem('error_1', JSON.stringify(true));
    });   
    $('#id_of_selector_2').parsley().on('field:error', function() {          
        localStorage.setItem('error_2', JSON.stringify(true));
    });
    $('#id_of_selector_2').parsley().on('field:success', function() {         
        localStorage.removeItem('error_2', JSON.stringify(true));
    });

You can call the localstorage item with some kind of boolean function since it has been stringified. 您可以使用某种布尔函数调用localstorage项,因为它已经被字符串化了。 I hope that helps you. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM