Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果第一次提交表单,则不会触发jQuery验证方法_Jquery_Jquery Plugins_Jquery Validate - Fatal编程技术网

如果第一次提交表单,则不会触发jQuery验证方法

如果第一次提交表单,则不会触发jQuery验证方法,jquery,jquery-plugins,jquery-validate,Jquery,Jquery Plugins,Jquery Validate,因此,我正在创建一个自定义插件,它使用jQuery验证作为基础。我的想法是创建一个插件来验证任何正常表单,而不必为所有表单创建单独的Jquery验证设置。这是我的密码 html 它正在工作,除了,如果表单是第一次提交,验证将不起作用,但是,如果我再次提交它,它现在可以正常工作。我似乎找不到问题,因为firebug上没有错误。任何帮助都将不胜感激使用.validate()方法只能初始化该插件。它不应该被包装在.submit()处理程序中,这就是问题的关键所在 仔细查看此页面的基本设置和用法:在成功

因此,我正在创建一个自定义插件,它使用jQuery验证作为基础。我的想法是创建一个插件来验证任何正常表单,而不必为所有表单创建单独的Jquery验证设置。这是我的密码

html

它正在工作,除了,如果表单是第一次提交,验证将不起作用,但是,如果我再次提交它,它现在可以正常工作。我似乎找不到问题,因为firebug上没有错误。任何帮助都将不胜感激

使用
.validate()
方法只能初始化该插件。它不应该被包装在
.submit()
处理程序中,这就是问题的关键所在


仔细查看此页面的基本设置和用法:

在成功编写利用jQuery验证插件的插件之前,首先需要了解jQuery验证插件的使用方法。换句话说,您显然不理解
.validate()
方法仅用于初始化插件。它不应该包装在
submit
处理程序中;这就是你问题的关键所在。我建议您仔细查看此页面:我也不明白首先执行所有这些操作的意义,因为jQuery验证插件将在没有插件的情况下自动获取内联HTML属性。hi@Sparky,ive fixed,ive将submit替换为each,自动初始化验证,而不是在表单提交时验证。谢谢同样,我不明白你定制插件的全部意义。这里没有它也是一样:是的@Sparky,我同意你的观点,但是我将使用大量定制的表单(使用数据属性)来控制服务器的响应,
<form role="form" class="form-horizontal form-validate" id="" action="/action/action.users.php">
    <div class="box-body">
        <h4>Account Information</h4>
        <div class="form-group">
            <label class="col-sm-3 control-label">test</label>
            <div class="col-sm-9 controls">
                <div class="row">
                    <div class="col-xs-12">
                        <input name="txt_username" class="form-control" placeholder="Username"  value="" type="text" data-required="true">
                    </div>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">test2</label>
            <div class="col-sm-9 controls">
                <div class="row">
                    <div class="col-xs-12">
                        <input name="txt_password" class="form-control" placeholder="Password" value="" type="text" data-required="true">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="box-footer">
        <button class="btn btn-sm btn-primary" type="submit">Submit</button>
        <button class="btn btn-sm btn-warning" type="reset">Reset</button>
    </div>
</form>
(function ( $ ) {
    $.fn.validate_form = function(options) {
        var settings = $.extend({
                action: "/",
                redirect: "",
                notification: "",
            }, options 
        );

        var $this = this;
        var post_data = {};
        var post_response = "default";

        /* if first submit, it wont trigger this, succeeding submits, it will run this */
        $this.validate({
            submitHandler:function(){
                $this.find(':input[type="text"],:input[type="radio"],:input[type="password"],:input[type="email"]').each(function(){
                    element = $(this);
                    var name = element.attr('name');
                    var val = element.val();

                    if(element.is(':radio')) {
                        if(element.is(':checked')) 
                            post_data[name]  = val;
                    }
                    else post_data[name]  = val;
                });

                console.log(post_data);

                $.post( settings.action, post_data, function(result){
                    post_response = result;
                    console.log(result);
                });
            }
        });
        /*end if first submit, it wont trigger this */

        $this.find('[data-required="true"]').each(function(){
            $(this).rules("add",{
                required: true
            });
        }); 

        return post_response;
    }
}( jQuery ));

$(document).ready(function() {
    $(".form-validate").submit(function(e){
        var action = $(this).attr("action");
        var result = $(this).validate_form({
            action:action
        });
        e.preventDefault();
    });
});