1

I have a form; I am using jQuery to post this form, but now I wonder how I can "reset" the form when I posted done?

My jQuery and PHP code:

<?php
chdir('../');
    include("_mysql.php");
    include("_settings.php");
    include("_functions.php");
chdir('admin');

if (isset($_POST['rubric'])) { 
    if (empty($_POST['rubric']) && empty($_POST['content'])) { 
        echo '<div class="alert alert-error alert-box">Du måste fylla i alla fält.</div>'; 
    }
    else { 
        $rubric = $_POST['rubric'];
        $sql = "INSERT INTO ".PREFIX."news(`date`, `poster`)
        VALUES('".time()."', '".$userID."')"; 
        mysql_query($sql);
        $id = mysql_insert_id();
        $sql2 = "INSERT INTO ".PREFIX."news_contents(`newsID`,  `headline`, `content`)
        VALUES('".$id."', '".$rubric."', '".$_POST['content']."')";
        mysql_query($sql2);
        echo '<div class="alert alert-box alert-success">Klart, nyheten postad.</div>'; 
    }
}
?>

    $("#form").submit(function(event) {
    event.preventDefault();
    var poster = $.post('addnews.php', $(this).serialize());
    poster.done(function(data) {
        $(".result").html(data);
    });
});

Does anyone know how I can do this? Because now when I post a new the rubric and content is still there, and I dont really know how to unset the variables.

And I really still want my error, and accept message left if possible.

4

2 回答 2

5

HTMLFormElements有一个reset函数,所以:

$("#form").submit(function(event) {
    var form = this;             // <=== Remember the form element

    event.preventDefault();
    var poster = $.post('addnews.php', $(this).serialize());
    poster.done(function(data) {
        $(".result").html(data);
        form.reset();            // <=== Use `reset`
    });
});
于 2013-07-08T07:36:05.333 回答
0

您可以使用表单reset()方法,例如

document.getElementById("form").reset();

或在 jQuery 中

$('#form').find('input:text, input:password, input:file, select, textarea').val('');
$('#form').find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
于 2013-07-08T07:54:10.363 回答