简体   繁体   English

通过JavaScript更新输入字段值后无法清除验证消息

[英]Cannot get validation message to clear after updating input field value via JavaScript

I have a form that I am validating on the client-side using the jQuery Validation plugin. 我有一个使用jQuery Validation插件在客户端进行验证的表单。 For brevity I have created a simple test case showing my issue. 为简便起见,我创建了一个简单的测试用例来展示我的问题。 The form has a single text input field and a single hidden input field. 表单具有单个文本输入字段和单个隐藏输入字段。

<script>
    function testThis() {
        alert('value before: ' + $("#testhidden").val());
        $("#testhidden").val("sometext");
        alert('value after: ' + $("#testhidden").val());
    }
</script>
<form name="testform" id="testform" method="post" action="">
    Enter Text: <input type="text" id="testfield" title="test field is required" name="testfield" size="20" minlength="2" maxlength="10" required="required" />
    <input type="hidden" id="testhidden" title="hidden field is required" name="testhidden" minlength="4" required="required" />
    <p><input type="button" id="addvalue" name="addvalue" value="Add Value via JavaScript" onclick="testThis();" /></p>
    <p><input type="submit" id="testbutton" name="testbutton" value="Submit Form" /></p>
</form>

Validation for both fields are that they are required and must be of a certain length; 对这两个字段的验证是必填字段,并且必须具有一定的长度; at least 4 characters for the hidden field and 2-10 characters for the text field. 隐藏字段至少4个字符,文本字段至少2-10个字符。 My caveat is that the hidden field is being updated via JavaScript after the form has been loaded to the DOM. 我的警告是,在将表单加载到DOM之后,隐藏字段将通过JavaScript更新。

I have created a fiddle to demonstrate my problem . 我制造了一个小提琴来证明我的问题 For this test I have added a button to simulate how I am modifying the hidden input's value via JavaScript. 在此测试中,我添加了一个按钮来模拟我如何通过JavaScript修改隐藏输入的值。 To test do this: 要进行测试,请执行以下操作:

  1. Try to submit the form without entering any text. 尝试提交表单而不输入任何文本。 You should get 2 validation errors. 您应该得到2个验证错误。
  2. Next enter text into the text input field and after entering at least 2 characters the first validation message should be hidden. 接下来,在文本输入字段中输入文本,输入至少2个字符后,第一条验证消息将被隐藏。
  3. If you try to submit at this point it still will not because of the hidden field requirement. 如果您此时尝试提交,由于隐藏字段的要求,仍然不会。
  4. Next click the Add value via JavaScript button. 接下来,单击通过JavaScript添加值按钮。 I am alerting the before and after values. 我提醒之前和之后的值。 At this point, once the hidden field has the correct data, I would like the validation message to be hidden but it is not. 在这一点上,一旦隐藏字段具有正确的数据,我希望验证消息被隐藏,但事实并非如此。
  5. At this point the form will submit because the validation criteria has been met but the error message is still displayed. 此时将提交表单,因为已满足验证条件,但仍显示错误消息。

How can I get the validation message to hide once the hidden field contains the appropriate text? 一旦隐藏字段包含适当的文本,如何获取验证消息以将其隐藏?

Here is the simple validation call that I am using to go along with the test form above: 这是我用于上面的测试表单的简单验证调用:

$("#testform").validate({errorElement:"div",ignore:[]});

In case it matters I am using jQuery 1.11.1 and jQuery Validate 1.12.0 万一重要,我正在使用jQuery 1.11.1和jQuery Validate 1.12.0

Update 更新

Although Pointy's answer does not solve the issue I think he is headed down the right path. 尽管Pointy的答案不能解决问题,但我认为他已经走上了正确的道路。 I need some way to trigger the validation to fire after updating the hidden field. 我需要某种方法来触发在更新隐藏字段后触发验证。 I have updated my fiddle now by adding a second input text field. 我现在通过添加第二个输入文本字段来更新小提琴 I am also updating this input field via JavaScript. 我也在通过JavaScript更新此输入字段。 When I trigger the blur() event on this new field after updating via JavaScript it correctly hides the validation message. 当我通过JavaScript更新后在此新字段上触发blur()事件时,它会正确隐藏验证消息。 Doing the same thing on the hidden field however still does not work. 但是,在隐藏字段上执行相同的操作仍然无效。 It definitely has something to do with it being a hidden field... 它绝对是一个隐藏的领域...

Quote OP : 引用OP

"I need some way to trigger the validation..." “我需要某种方式来触发验证...”

The plugin provides a method called .valid() in which its sole purpose is to programmatically trigger validation, either on a single field or the entire form. 该插件提供了一种称为.valid()的方法 ,其唯一目的是以编程方式触发对单个字段或整个表单的验证。

"It definitely has something to do with it being a hidden field" “它肯定与它是一个隐藏领域有关”

The validation of a regular input field is normally triggered by events such as keyup and blur. 常规输入字段的验证通常由诸如击键和模糊之类的事件触发。 Since you don't have those events on a hidden field, you simply need to use the .valid() method to manually trigger validation. 由于在隐藏字段中没有这些事件,因此只需要使用.valid()方法即可手动触发验证。

$("#testhidden").valid();

I've modified your function as follows and since you're using jQuery, also removed the inline JavaScript... 我已经按如下方式修改了您的函数,并且由于您使用的是jQuery,因此还删除了内联JavaScript ...

$('#addvalue').on('click', function() {
    alert('value before: ' + $("#testhidden").val());
    $("#testhidden").val("sometext");
    $("#testhidden").valid(); // <- manually trigger validation of this field
    alert('value after: ' + $("#testhidden").val());
});

DEMO: http://jsfiddle.net/opzg6uxn/2/ 演示: http : //jsfiddle.net/opzg6uxn/2/


However, I don't understand why you'd need to validate something beyond the control of the user. 但是,我不明白为什么您需要验证用户无法控制的内容。 In other words, since the hidden field is controlled programmatically, what's the point of applying validation to it in the first place? 换句话说,由于隐藏字段是通过程序控制的,因此首先对其进行验证有什么意义?

You have to trigger the "change" event yourself to cause the validation code to re-run. 您必须自己触发“更改”事件,以使验证代码重新运行。 Here is your fiddle with that change. 这是您所做的更改的小提琴。

    $("#testhidden").val("sometext").trigger("change");

To make it work on jsfiddle, I changed the type of you're hidden input to a simple text type and actually added the style visibility:hidden to make it invisible for the user. 为了使其能够在jsfiddle上运行,我将隐藏输入的类型更改为简单的文本类型,并实际上添加了样式visible:hidden以使其对用户不可见。 With this approach, the validation plugin of jquery will evaluate you're input even if it is invisible for the user. 通过这种方法,即使用户看不见jquery的验证插件,它也会评估您的输入。

<input type="text" style="visibility:hidden" id="testhidden" title="hidden field is required" name="testhidden" minlength="4" required="required" />

Hope this answer to you're problem. 希望这个答案对您有问题。

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

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