1 回答
TA贡献1906条经验 获得超3个赞
只需使用 e.preventDefault(); 作为 onsubmit 函数的第一步:
// when the form is submitted
$(function () {
// init the validator
$('#contact-form').validator();
// when the form is submitted
$('#contact-form').on('submit', function (e) {
e.preventDefault();
// if the validator does not prevent form submit
var url = "contact.php";
// POST values in the background the the script URL
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
// data = JSON object that contact.php returns
// we recieve the type of the message: success x danger and apply it to the
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
// let's compose Bootstrap alert box HTML
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
// If we have messageAlert and messageText
if (messageAlert && messageText) {
// inject the alert to .messages div in our form
$('#contact-form').find('.messages').html(alertBox);
// empty the form
$('#contact-form')[0].reset();
}
}
});
return false;
})
});
- 1 回答
- 0 关注
- 152 浏览
添加回答
举报