0

我正在使用 Bootstrap 创建一个简单的联系表单,并希望在按下“提交”按钮时实现以下目标:

  • 使用 jQuery 验证表单字段
  • 如果验证通过,则提交表单,使用 PHP 作为处理器
  • 在 Bootstrap 模式对话框中显示一条消息,说明表单已通过电子邮件发送

知道我的 PHP 和 JS 知识低于平均水平,我很高兴地说我已经能够让所有项目单独工作,但我无法将它们组合在一起......

你能给我一些意见吗?

验证用途:

var validator = $("#contact").validate({
  ...

PHP 使用:

mail()

使用这样的按钮提交表单时,两者都有效:

<button type="submit" name="submit" id="submitButton" class="btn btn-bcome pull-right">Send</button>

为了激活对话框,我将按钮更改为:

<button href="#submit" role="button" button type="submit" name="submit" id="submitButton" class="btn btn-bcome pull-right" data-toggle="modal">Send</button>

这使得模态正确显示,但不会发生验证和提交。

我找到了一个相关的帖子: 出现在表单提交上的 jQuery Modal。但是我不知道要专门针对我的情况进行调整。


我试图调整你告诉我的内容,这就是我得到的:Html 部分:

button href="#submit" role="button" button type="submit" name="submit" id="submitButton" class="btn btn-bcome pull-right" data-toggle="modal">Envoyer</button>

<div id="submit" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">Thanks for your mail</h3>
      </div>
      <div class="modal-body">
        <p>We will get back to you soon</p>
      </div>
      <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
      </div>
    </div>

在我的 JS 文件中:

var $validator = {};

$(document).ready(function()
{
    // validate signup form on keyup and submit
    var validator = $("#contact").validate({
       submitHandler: function(form)
        {
            //code to submit your form here
            //code to open your modal here
        errorClass:'error',
        validClass:'success',
        errorElement:'span',
        highlight: function (element, errorClass, validClass) { 
            $(element).parents("div[class='clearfix']").addClass(errorClass).removeClass(validClass);
        }, 
        unhighlight: function (element, errorClass, validClass) { 
            $(element).parents(".error").removeClass(errorClass).addClass(validClass); 
        },
        rules: {
            fname: {
                required: true,
                minlength: 2
            },
            name: {
                required: true,
                minlength: 2
            },
            email: {
                required: true,
                email: true
            },
            message: {
                required: true,
                minlength: 10
            }
        },
        messages: {
            fname: {
                required: '<span class="help-inline">First name.</span>',
                minlength: jQuery.format('<span class="help-inline">2 chars</span>')
            },
            name: {
                required: '<span class="help-inline">Name.</span>',
                minlength: jQuery.format('<span class="help-inline">2 chars</span>')
            },
            email: {
                required: '<span class="help-inline">Email.</span>',
                email: '<span class="help-inline">Ex : name@exemple.com</span>'
            },
            message: {
                required: '<span class="help-block">Message</span>',
                minlength: jQuery.format('<span class="help-block">10 chars</span>')

        $('form').submit(function() {
            if ($validator.numberOfInvalids() > 0) {
                $('#submit').modal('hide');
            } else {
                $('#submit').modal('show');
            }
        }
    });
}
);

我肯定遗漏了一些东西,但目前,只有模式部分被执行,而不是验证部分或提交表单部分。

知道我应该怎么做才能完成这项工作吗?

4

1 回答 1

0

如果您使用的是 bassastance 的 jquery 验证插件,那么您正在寻找 submitHandler 选项,该选项在此处定义:http: //docs.jquery.com/Plugins/Validation/validate#toptions

$(document).ready(function()
    {
        // validate signup form on keyup and submit
        var validator = $("#contact").validate({
           submitHandler: function(form)
            {
                //code to submit your form here
                //code to open your modal here
            }
        });
    }
);

更新:我正在根据 Fred 提供的进一步代码更新此答案。

Javascript:

var $validator; //declared for further use later

$(document).ready(function()
{
    // validate signup form on keyup and submit
    $validator = $("#contact").validate({
        errorClass:'error',
        validClass:'success',
        errorElement:'span',
        highlight: function (element, errorClass, validClass) { 
            $(element).parents("div[class='clearfix']").addClass(errorClass).removeClass(validClass);
        }, 
        unhighlight: function (element, errorClass, validClass) { 
            $(element).parents(".error").removeClass(errorClass).addClass(validClass); 
        },
        rules: {
            fname: {
                required: true,
                minlength: 2
            },
            name: {
                required: true,
                minlength: 2
            },
            email: {
                required: true,
                email: true
            },
            message: {
                required: true,
                minlength: 10
            }
        },
        messages: {
            fname: {
                required: '<span class="help-inline">First name.</span>',
                minlength: jQuery.format('<span class="help-inline">2 chars</span>')
            },
            name: {
                required: '<span class="help-inline">Name.</span>',
                minlength: jQuery.format('<span class="help-inline">2 chars</span>')
            },
            email: {
                required: '<span class="help-inline">Email.</span>',
                email: '<span class="help-inline">Ex : name@exemple.com</span>'
            },
            message: {
                required: '<span class="help-block">Message</span>',
                minlength: jQuery.format('<span class="help-block">10 chars</span>')
            }
        },
        submitHandler: function(form) //submitHandler is an option taken by the validate function
        {
            //i put your submission code in here

            $('form').submit(function() {
                if ($validator.numberOfInvalids() > 0) 
                {
                    $('#submit').modal('hide');
                } else 
                {
                    $('#submit').modal('show');
                }
            });
        }
    });
});
于 2013-01-01T01:20:50.533 回答