如何在不使用提交按钮的情况下触发验证?

时间:2022-08-23 22:32:16

I am using the jQuery Validation plugin and trying to trigger the validation/submit with an alternate button. I would like to create a jquery function that will change the css, and then run the validate function. I have looked at previously answered questions but have not found any that address this issue with regards to the Validation plugin and and the submitHandler function. Any suggestions?

我正在使用jQuery验证插件,并尝试使用一个备用按钮来触发验证/提交。我想创建一个jquery函数来更改css,然后运行validate函数。我查看了之前回答的问题,但是没有找到任何关于验证插件和submitHandler函数的问题。有什么建议吗?

UPDATE TO QUESTION: The button that I would like to use is placed outside of the form. What is the best way to submit and validate a form with a button located outside of that form?

更新到问题:我想要使用的按钮放在表单之外。使用位于表单外部的按钮提交和验证表单的最佳方式是什么?

Here is the code:

这是代码:

$("#edit-button").click(function(event) {

$('#postAccordion2').css('padding-bottom', '0px');
$('#edit-button').css('display','none');
$("#applicant-form").validate({
    submitHandler: function(form) {
            $(form).ajaxSubmit({                  
                type: "POST",
                data: {
                    "firstName" : $('#firstName').val(),
                    "lastName" : $('#lastName').val()
                    },
                dataType: 'json',
                url: './includes/ajaxtest.php',
                error: function() {alert("doh!");},
                success: function() {alert("yippee!");},

    }    

  });

return false;   
   },
        errorPlacement: function(error,element) {
                        return true;
                },
        rules: {
            "firstName": {
                required: true,
                minlength: 1
                },  
            "lastName": {
                required: true
                }
        }


});
   });

2 个解决方案

#1


19  

You should be able to just call valid on whatever element you like:

您应该能够对您喜欢的任何元素调用valid:

$('selector').valid();

Here are some references:

以下是一些参考:

http://docs.jquery.com/Plugins/Validation/valid

http://docs.jquery.com/Plugins/Validation/valid

http://docs.jquery.com/Plugins/Validation/validate

http://docs.jquery.com/Plugins/Validation/validate

#2


8  

You have to call validate with no arguments to trigger the validate function, like this:

您必须在没有参数的情况下调用validate来触发validate函数,如下所示:

$("#applicant-form").validate();

#1


19  

You should be able to just call valid on whatever element you like:

您应该能够对您喜欢的任何元素调用valid:

$('selector').valid();

Here are some references:

以下是一些参考:

http://docs.jquery.com/Plugins/Validation/valid

http://docs.jquery.com/Plugins/Validation/valid

http://docs.jquery.com/Plugins/Validation/validate

http://docs.jquery.com/Plugins/Validation/validate

#2


8  

You have to call validate with no arguments to trigger the validate function, like this:

您必须在没有参数的情况下调用validate来触发validate函数,如下所示:

$("#applicant-form").validate();