本文介绍了错误位置-jQuery验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据字段中输入的详细信息更改错误位置.也就是说,如果该字段为空,则在提交按钮后显示错误,否则,如果该字段不为空,但最小长度或最大长度与元素后的显示错误不匹配.

How to change error placement according to the details entered in field. i.e if field is empty then display error after submit button else if the field is not empty but the min length or max length is not matched the display error after element.

我尝试了这个,但是没有用.

I tried this but its not working.

errorPlacement: function(error, element) {
if(error.text() === "Please fill the field.")
{
  if(element.val() === ""){
    $(".login-empty-error").html(error);
  } else {
    error.insertAfter(element);
  }
} else {
  error.insertAfter(element);
}
}

如何查找是否需要该类型?

How to find whether the type is required?

推荐答案

errorPlacement选项无法执行此类操作.一旦使用errorPlacement将错误元素容器放置在布局中,插件便会根据需要简单地切换此元素.由于该元素已经存在但已隐藏,因此其位置不会更改.

The errorPlacement option cannot do anything like this. Once the error element container is placed within the layout using errorPlacement, the plugin simply toggles this element as needed. Its location is not changed as the element is already there but hidden.

换句话说,您无法使用提供的任何选项或方法在表单周围动态移动错误元素.

In other words, you cannot dynamically move the error element around the form using any of the provided options or methods.

但是,如果您希望表单中出现错误时在按钮附近显示一条消息,请查看 showErrors选项.

However, if you want a message to appear near the button when there are errors within the form, look at the showErrors option.

showErrors: function (errorMap, errorList) {
    if (this.numberOfInvalids() > 0) {
        $("#summary").html("Your form contains " + this.numberOfInvalids() + " errors, see details above.");
    } else {
        $("#summary").empty();
    }
    this.defaultShowErrors(); // <- default messages that appear within form
}

基本用法演示: http://jsfiddle.net/v2h2a2mb/

Basic usage demo: http://jsfiddle.net/v2h2a2mb/

您可以调整此功能,以便仅在特定条件下显示一条消息.内置参数如下所述.

You could tweak this function so that a message only appears under certain conditions. The built-in arguments are described below...

errorMap ,类型:对象键/值对(键指的是输入字段的名称)对要为该输入显示的消息进行赋值.

errorMap,Type: Object,Key/value pairs, where the key refers to the name of an input field, values the message to be displayed for that input.

错误列表,类型:数组所有当前已验证元素的数组.包含具有以下两个属性的对象:

errorList,Type: Array,An array for all currently validated elements. Contains objects with the following two properties:

  • 消息,类型:字符串,要显示的输入消息.

  • message,Type: String,The message to be displayed for an input.

元素,类型:元素此项的DOMElement.

element,Type: Element,The DOMElement for this entry.

这篇关于错误位置-jQuery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:44