Home >Web Front-end >JS Tutorial >jquery validate bug solution under ie8_jquery
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.
Then it is natural to use the valid() function of the plug-in:
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:
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:
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.
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.
Oh, by the way, don’t forget to block the form’s default event at the end.