jQuery Setting a validation error box with two classes

喜夏-厌秋 提交于 2019-12-25 08:35:13

问题


I have an with my code and I have one particular textarea that I am using the main .error class and another .textAreaError that places the error box were I want it but I am unsure how I would configure the textarea error to load in this specific box.

From a previous question I was given the following jQuery line function(error, element) { error.appendTo(element.siblings("label")); } but I am unsure how to work it into the code below.

jQuery:

jQuery(document).ready(function() { 
    //Home Validation
    onfocusout: true,
    $("#quote").validate({
        rules:{
            companyName:{
                required: true,
                url: true
            }
        }
    });

HTML:

<div class="error textAreaError">Error Here</div>


回答1:


From looking at the documentation for the validate plugin, it looks like you can control the position of the error with the errorPlacement argument which uses function(error, element). The code would look something like this:

jQuery(document).ready(function() { 
    //Home Validation
    $("#quote").validate({
        onfocusout: true,
        rules:{
            companyName:{
                required: true,
                url: true
            }
        },
        errorPlacement: function(error, element) {
            if (element.is("textarea"))
                error.appendTo(".textAreaError");
            else
                error.appendTo(".generalError");
        }
    });
});

You'd have to give the general error box the generalError class. Or, give the error boxes id's and refer to those instead (e.g. "#textAreaError" and "#generalError").



来源:https://stackoverflow.com/questions/10200250/jquery-setting-a-validation-error-box-with-two-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!