Disable jQuery Validate plugin

心不动则不痛 提交于 2020-01-03 04:23:27

问题


I'm completing a form using Web driver and need to bypass two form fields by using browser.execute_script() to insert a hidden form field into the DOM.

However I also need to disable existing validation methods in order to submit the form. The form uses jQuery Validate. Can anybody advise how to disable part or all of jQuery Validate?

I tried using the following, but I get the error TypeError: d is undefined

b.execute_script('
    $("firstMemorableCharacter").rules("remove");
    $("secondMemorableCharacter").rules("remove");
')

Where firstMemorableCharacter is the name of the form field which has attached validation rules.

The validation is set up using jQuery Validate as follows:

with($.validator) {
    addMethod(...);

setDefaults({
    ....
    messages: { },
    rules: {
            firstMemorableCharacter: {
                radioHasValue: true
            },
            secondMemorableCharacter: {
                radioHasValue: true
            }
    }
});

(The rules shown above are the ones that I need to disable.)

Thanks in advance.

UPDATE:

Following this post, How do I remove jQuery validation from a form? , I tried submitting the form using the HTML DOM (bypassing jQuery) using $("#form-id")[0].submit();. However, this does not work - It seems there is some additional JavaScript within jQuery that needs to be executed in order for the form to submit properly.

Therefore I need a way of specifically disabling just these two rules within jQuery Validate, rather than disabling the whole of jQuery or jQuery Validate.


回答1:


Can anybody advise how to disable part or all of JQuery Validator?

Once initialized, you cannot disable any part of it. However, you can dynamically manipulate the rules using the .rules() method as you've already guessed.

Just make sure the .rules() method is called sometime after .validate() or it will fail.

See this simple DEMO: http://jsfiddle.net/qdjMS/


There is a syntax error in your code below.

$("firstMemorableCharacter").rules("remove");
$("secondMemorableCharacter").rules("remove");

Notice that your two jQuery selectors are meaningless.

If you're trying to target an id, then you'll need a # in front...

$("#firstMemorableCharacter").rules("remove");
$("#secondMemorableCharacter").rules("remove");

From your comments, I also noticed that your element contains id="firstMemorableCharacter1", so you also need to spell the id exactly the same...

$("#firstMemorableCharacter1").rules("remove");

Otherwise, you could just target them by name as also shown in my demo...

$("[name='firstMemorableCharacter']").rules("remove");
$("[name='secondMemorableCharacter']").rules("remove");


来源:https://stackoverflow.com/questions/15825768/disable-jquery-validate-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!