javascript Jquery Validate插件规则方法不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19683681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:28:12  来源:igfitidea点击:

Jquery Validate plugin rules method not working

javascriptjqueryjquery-validate

提问by th1rdey3

I have a asp.netform where I am trying to use Jquery Validation plugin. I am trying to add the rules using javascript using the rulesmethod. so i tried something like this

我有一个asp.net表单,我试图在其中使用 Jquery 验证插件。我正在尝试使用 javascriptrules方法添加规则。所以我尝试了这样的事情

$('#btnSave').on('click', function () {
    $("#txtAmount").rules("add", {
        required: true,
        minlength: 2,
        messages: {
            required: "Required input",
            minlength: jQuery.format("Please, at least {0} characters are necessary")
        }
    });
    $('form').validate({
        submitHandler: function (form) {
            form.submit();
        }
    });
});

I have set the ClientIDMode="Static"so that i can use the contorl IDs directly. However, this is not working. Can anyone tell me where is the problem. I can't seem to put my finger on it.

我已经设置了ClientIDMode="Static"以便我可以直接使用控制 ID。但是,这是行不通的。谁能告诉我问题出在哪里。我似乎无法将手指放在上面。

here is a JsFiddle

这里有一个 JsFiddle

回答by Sparky

Yes, as explained in the accepted answer, you must call .validate()(initialization) before calling the .rules()method.

是的,如已接受的答案中所述,您必须.validate()在调用该.rules()方法之前调用(初始化)。

However:

然而:

1) Since .validate()is only the initialization method (only gets called once), there is no need to wrap it inside a clickhandler. The plugin already automaticallycaptures the click event.

1) 由于.validate()只是初始化方法(只被调用一次),因此无需将其包装在click处理程序中。该插件已经自动捕获点击事件。

2) In this example case there is no need for a submitHandlerthat only contains a form.submit()inside, since that's already the default behavior of the submitHandler. Leaving the submitHandlerout entirely will yield the same result. Although, if you need to use ajax or perform some other code on submit, then by all means, use the submitHandler.

2) 在这个例子中,不需要submitHandler只包含form.submit()内部的a ,因为这已经是submitHandler. submitHandler完全离开会产生相同的结果。虽然,如果您需要使用 ajax 或在提交时执行一些其他代码,那么请务必使用submitHandler.

$(document).ready(function() {

    $('form').validate({
        // other rules and options
    });

    $("#id1").rules("add", {
        required: true,
        minlength: 2,
        messages: {
            required: "Required input"
        }
    });

});

DEMO: http://jsfiddle.net/85KR4/5/(regular form action called on successful submission)

演示:http: //jsfiddle.net/85KR4/5/(成功提交时调用的常规表单操作)

DEMO 2: http://jsfiddle.net/85KR4/7/(ajax called on successful submission)

DEMO 2:http: //jsfiddle.net/85KR4/7/(提交成功调用ajax)

回答by Smileek

Everything is correct except the order. At first you should call validate, and just after that you can call rules.

除了顺序,一切都是正确的。一开始你应该打电话validate,然后你就可以打电话了rules

http://jsfiddle.net/85KR4/3/

http://jsfiddle.net/85KR4/3/