Home >Web Front-end >JS Tutorial >jquery validate bug solution under ie8_jquery

jquery validate bug solution under ie8_jquery

WBOY
WBOYOriginal
2016-05-16 17:15:301105browse

The project uses jquery's form verification plug-in validate. Before, it has only done very simple verification and has not done any more complex applications. Recently, the project's requirements for applications have increased. There are two submit buttons on a page, and then the form The verification is bound to the click event of the button, as follows.
jquery validate bug solution under ie8_jquery
Then it is natural to use the valid() function of the plug-in:
jquery validate bug solution under ie8_jquery
This can indeed achieve the desired effect, but there is a problem under ie8, valid () method always returns false, and all fields will be verified as required fields. I struggled for a long time because the jquery plug-in code is very complicated, so it was very difficult to read at first. Later, I checked step by step and found the problem. The problem It comes from the attributeRules() function:
jquery validate bug solution under ie8_jquery
The function of this function is: when you write the validation rules on the page instead of in the script, you can also apply the validation framework. Technically it is very reasonable, and the processing of required is also reasonable, but for ie8, there is a bit of a problem. IE8 will execute the following branch:
jquery validate bug solution under ie8_jquery
Therefore, all fields will be verified as required fields. After testing, there are two final methods to solve the problem:

The first is to find the following code in the rules() method and comment out the call to attributeRules(). The reason why this can be done is because Generally, verification is rarely written into the page. Of course, this is definitely not the best solution, so let’s take a look at the second one.
jquery validate bug solution under ie8_jquery
The second solution requires two things: First, you need to make some modifications to the attributeRules() method, replacing the getAttribute() method with the attr() method. Note that getAttribute() is a js method, and attr() is a jquery method. Their usage objects are different. In fact, after this change, the bug under ie8 has been solved, but this problem has occurred again under ie7, so You need to use the latest jquery. When I tested, I used jquery 1.10.2 version.
jquery validate bug solution under ie8_jquery
Oh, by the way, don’t forget to block the form’s default event at the end.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn