jQuery - 在提交时隐藏表单

时间:2011-07-23 14:53:26

标签: jquery forms jquery-validate submit hide

我想在成功提交时隐藏我的表格:

以下是测试空间的链接:http://www.bgv.co.za/testspace/contact_3.php

它是PHP jQuery mongrel的组合。目前我正在使用jQuery验证器,我已经添加了自定义脚本来更改输入字段包装div的类 - 作为显示必填字段的方法。

这是我在PHP中的内容

<?php

$ subject =“网站联系表格查询”;

//如果表单已提交 if(isset($ _ POST ['submit'])){

//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
    $hasError = true;
} else {
    $name = trim($_POST['contactname']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
    $hasError = true;
} else {
    $email = trim($_POST['email']);
}

//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['message']));
    } else {
        $comments = trim($_POST['message']);
    }
}

//If there is no error, send the email
if(!isset($hasError)) {
    $emailTo = 'info@bgv.co.za'; //Put your own email address here
    $body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;

}

} ?&GT;

这是脚本区域:

$(document).ready(function(){
$('#contactform').validate({
        showErrors: function(errorMap, errorList) {

            //restore the normal look
            $('#contactform div._required').removeClass('_required').addClass('xrequired');

            //stop if everything is ok
            if (errorList.length == 0) return;

            //Iterate over the errors
            for(var i = 0;i < errorList.length; i++)
                $(errorList[i].element).parent().removeClass('xrequired').addClass('_required');
        }

}); });

因此,当有人提交有效表单时,您会在联系表单下看到一个新标题 - 从此处开始:

        <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
    <h1 class="success_form">Thank You!</h1>
    <?php } ?>  

及以下是输入表格的数据。

但它的形式如下所示,我想隐藏......请帮助:)。

感谢Flashbackzoo

,这是我添加的新位
        $("#content").empty();
    $("#content").append(
    "<p>If you want to be kept in the loop...</p>" +
    "<p>Or you can contact...</p>"
    );
    $('h1.success_').removeClass('success_').addClass('success_form');
    $('#contactform').hide();

},

1 个答案:

答案 0 :(得分:2)

添加提交处理程序...

$('#contactform').validate({
    showErrors: function(errorMap, errorList) {
        // your code...
    },
    submitHandler: function() {
        $('#contactform').hide();
    }
});

<强>更新

回应你的评论Brett。您可以将submitHandler更改为......

submitHandler: function() {
    $("#content").empty();
    $("#content").append(
        "<p>If you want to be kept in the loop...</p>" +
        "<p>Or you can contact...</p>"
    );
}

这将使用.empty()方法从#content中删除所有子元素。然后使用.append()方法将元素添加到#content。您可以将#content替换为您喜欢的任何选择器。