消息发送后清除表单输入

时间:2015-05-21 12:12:37

标签: javascript jquery ajax

我试图在表单发送后清除表单消息输入字段。虽然它有机会在发送之前清除数据。 这是我的代码:

-or

FORM:

<script>
    $(function () {
        $('form#SendForm').on('submit', function (e) {
            $.post('sendmessagefriend.php', $(this).serialize(), function (data) {
                // This is executed when the call to mail.php was succesful.
                // 'data' contains the response from the request
            }).error(function () {
                // This is executed when the call to mail.php failed.
            });
            e.preventDefault();
        });
    });
</script>
<script>
    $('#SendForm').on('click', function () {
        $('#SendForm').find('input:text').val('');
        $('input:checkbox').removeAttr('checked');
    });
</script>   

1 个答案:

答案 0 :(得分:0)

而不是单击处理程序,请将此

$('#SendForm').find('input:text').val(''); 
$('input:checkbox').removeAttr('checked');

成功回调,如

$('form#SendForm').on('submit', function(e) {
        $.post('sendmessagefriend.php', $(this).serialize(), function (data) {
            // This is executed when the call to mail.php was succesful.
            // 'data' contains the response from the request
            $('#message').val(''); 
        })
        .error(function() {
            // This is executed when the call to mail.php failed.
        });
        e.preventDefault();
    });

如果您不关心请求是否成功,只是想清除输入,请将其放在您的ajax调用之后

$('form#SendForm').on('submit', function(e) {
            $.post('sendmessagefriend.php', $(this).serialize(), function (data) {
                // This is executed when the call to mail.php was succesful.
                // 'data' contains the response from the request
            })
            .error(function() {
                // This is executed when the call to mail.php failed.
            });

            $('#message').val('');  
            e.preventDefault();
        });