我正在使用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 that appears on form submit。 但我不知道要具体适应我的情况。
我试图改编你告诉我的内容,这就是我得到的: 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');
}
}
});
}
);
我肯定遗漏了一些东西,但目前只执行模态部分而不是验证部分或提交表单部分。
知道我应该怎么做才能使这项工作?
答案 0 :(得分: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');
}
});
}
});
});