It's entirely unclear by the lack of code in your question, but I think you might misunderstand where to attach the .valid()
method.
This method only gets attached to a selector representing a single <form>
element...
$('#myform').valid(); // triggers validation on the entire form
Or to any selector that represents a single form input element...
$('#myform input[name="myinput"]').valid(); // triggers validation on one input element
When .valid()
is called, validation is triggered on the selected element, error message(s) is/are displayed, and the method will return true
or false
.
The .valid()
method can never be attached to a selector that represents more than one element, or a collection of elements without also using a jQuery .each()
.
EDITS:
showErrors
is just for showing existing errors. You cannot "invalidate" a field by calling showErrors
. Fields are either valid or invalid based solely on what is contained within that field.
You've tagged the question with Unobtrusive Validation. Since jQuery Validation is handled automatically via the Unobtrusive Validation plugin, you are not allowed to call your own instance of the .validate()
method. That's because the Unobtrusive plugin automatically constructs and calls .validate()
. Once it's called, the plugin is initialized, it cannot be called again, and all subsequent calls are always ignored.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…