A solution to the caching problem for Jquery validation Remote authentication _jquery

Source: Internet
Author: User
The recent project has been using jquery validation to make checksums, combining bootstrap and jquery form to feel good. But the remote verification cache problem has been a headache for me for a long time, almost as if the plugin had been abandoned.

An example is provided:

Existing schools, grades, class entities

In the same school can not have the same grade, in the same grade can not have the same class

In the same school can not have the same grade as an example, at the time of validation by remote mode:

Js
Copy Code code as follows:

"Gradeid": {
Required:true,
min:0,
Remote: {
URL: "Grademanager-checkgrade.action",
Type: "Post",
DataType: "JSON",
Data: {
' Gradeid ': function () {
Return $ ("#gradeId"). Val ();
},
' SchoolID ': function () {
Return $ ("#schoolId"). Val ();
}
}
}
}

Html
Copy Code code as follows:

<div class= "Control-group" >
<label class= "Control-label" for= "SchoolID" > School </label>
<div class= "Controls" >
<s:select name= "SchoolID" list= "Schools" listkey= "SchoolID"
Listvalue= "Schoolname" headerkey= "-1" headervalue= "Please choose School" ></s:select>
</div>
</div>

<div class= "Control-group" >
<label class= "Control-label" for= "Gradeid" > Grade </label>
<div class= "Controls" >
<s:select name= "Gradeid" list= "grades" listkey= "Gradeid"
Listvalue= "Gradename" headerkey= "-1" headervalue= "Please select Grade" ></s:select>
</div>
</div>

Now there are test schools 1, test Schools 2, two schools, of which the test school 1 have primary one grade, primary school second grade two grade

Theoretically, in the new grade, if the school chooses the test school 1, then the choice of primary one grade, primary school second year, remote verification will be the error "the Grade already exists"

In fact, the first operation, the result is also correct

But if you change schools at this time, such as choosing a test school 2, because of the cache problem, the validation plug-in will not be able to rerun the new remote verification and will return directly to the results of the previous validation, so there are the following egg pain error

This is not the most difficult to accept, the more serious problem if you first choose the test School 2, and primary school first grade, remote verification through, at this time again Select Test School 1,remote verification still pass, so that the same school can not have the same grade of verification is tantamount to failure


So a variety of query solutions

It is said that "Cache:false" was added to the remote, but the measurement did not solve the problem.

Tried to tie the onchange event to the school drop-down box. Empty Gradeid value, still unable to solve the problem

Review the validation API and discover that the. Valid () method can trigger validation proactively, but remote still does not have a validation due to caching problems

Also looked at the source of the validation, found that there is a previousvalue this thing, if there is a value will be directly return the previous validation results

So I tried to empty the value.
Copy Code code as follows:

$ ("#schoolId"). Change (function () {
$ ("#gradeId"). Removedata ("Previousvalue");
});

Successfully solve the problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.