在一个文本框中输入值后,所有错误消息均消失 [英] All error messages disappear when value is entered on one textbox

查看:76
本文介绍了在一个文本框中输入值后,所有错误消息均消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有jquery validate的以下形式.

I have following form with jquery validate.

  1. 当我提交表单而不输入任何值时,它显示错误消息.但是,当我在金额"文本框中输入值时,两个错误消息都消失了.

  1. When I submit the form without entering any values, it shows error messages. But when I enter value in "Amount" textbox, both the error messages are disappeared.

此外,此后,当我清除金额"文本框中的值时,仅显示金额"错误消息,直到单击按钮为止.

Also, after that, when I clear value in the Amount textbox, only the "Amount" error message is displayed until I click the button.

该如何解决?

小提琴

jQuery代码

$("#formAddPayment").validate({
  rules: {
    "ddlModeOfPayment": {
      required: true
    },
    "txtAmount": {
      required: true
    }
  },
  messages: {
    "ddlModeOfPayment": {
      required: "Please select  Mode of Payment."
    },
    "txtAmount": {
      required: "Please enter Amount."
    }
  },

  showErrors: function (errorMap, errorList) {

    var errorListResult = $.map(errorList, function(error){
      return "<li>" + error.message+ "</li>";
    });

    $('#errorSummaryList').html(errorListResult.join(''))
    $('#errorSummaryList').fadeIn('fast');

    //Show error adjuscent to control also
    //this.defaultShowErrors();
  },
  submitHandler: function (form) {
    return false;
  }
});

推荐答案

在一个文本框中输入值后,所有错误消息都会消失

All error messages disappear when value is entered on one textbox

来自 showErrors 的文档:

From the documentation for showErrors:

参数仅包含 个已被当前验证的元素,在进行验证 onblur时可以单个元素 focusoutkeyup上.

The arguments contain only those elements currently validated, which can be a single element when doing validation onblur on focusout or keyup.

如您所见,showErrors选项正在按设计工作.新验证完成后,新的消息map不会保留先前的消息.如果验证了一个字段,则地图仅包含该字段的消息,依此类推.

As you can see, the showErrors option is working as designed. The new map of messages does not retain the previous messages after new validation is completed. If one field is validated, then the map only contains the message for the one field, etc.

只需使用 wrappererrorLabelContainer 选项,它们一起使用时旨在自动保留整个表单中待处理邮件的更新列表...

Just use wrapper and errorLabelContainer options, which, when used together, were designed to automatically keep an updated list of pending messages for the whole form...

wrapper: "li",  // <- the LABEL will be inside of this
errorLabelContainer: "#errorSummaryList",  // <- your UL element

演示: jsfiddle.net/y9o8du3q/

DEMO: jsfiddle.net/y9o8du3q/

这篇关于在一个文本框中输入值后,所有错误消息均消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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