如何触发验证,而无需使用一个提交按钮 [英] How to trigger validation without using a submit button

查看:256
本文介绍了如何触发验证,而无需使用一个提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery验证插件,并试图触发验证/提交一个替代按钮。我想创建一个jQuery函数,将改变CSS,然后运行验证功能。我已经看过previously回答问题,但还没有找到任何解决这个问题的问候验证插件,以及submitHandler功能。有什么建议么?

更新质疑:我想用放置在窗体外部的按钮。什么是与位于该形式之外的按钮,提交并验证一个表单的最佳方法是什么?

下面是code:

  $(#编辑按钮)。点击(函数(事件){

$('#postAccordion2)的CSS('填充底,0px​​)。
$('#编辑键),CSS('显示器','无')。
$(#申请表格」)。验证({
    submitHandler:功能(形式){
            $(形式).ajaxSubmit({
                键入:POST,
                数据: {
                    名字:$('#的firstName)VAL()。
                    姓氏:$('#的lastName)VAL()。
                    },
                数据类型:JSON,
                网址:./includes/ajaxtest.php,
                错误:函数(){警报(卫生署!);},
                成功:函数(){警报(yippee的!);},

    }

  });

返回false;
   },
        errorPlacement:功能(错误,元素){
                        返回true;
                },
        规则:{
            名字: {
                要求:真实,
                MINLENGTH:1
                },
            姓: {
                要求:真实的
                }
        }


});
   });
 

解决方案

您应该能够只是调用有效的任何元素你喜欢的:

  $('选择')有效的()。
 

下面是一些参考资料:

<一个href="http://docs.jquery.com/Plugins/Validation/valid">http://docs.jquery.com/Plugins/Validation/valid

<一个href="http://docs.jquery.com/Plugins/Validation/validate">http://docs.jquery.com/Plugins/Validation/validate

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?

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
                }
        }


});
   });

解决方案

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

$('selector').valid();

Here are some references:

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

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

这篇关于如何触发验证,而无需使用一个提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆