Ajax 表单需要在提交后清除

Ajax Form needs to be cleared after submission

本文关键字:提交 清除 表单 Ajax      更新时间:2023-09-26

Ajax 表单需要在提交后清除。请如何只清除文本字段学生帖子,表单变量设置为发布 ajax 表单提交后的文本文本无需清除任何其他表单输入(假设它们已到位)。下面是我的工作 Ajax 表单

this.postMessage = function(user, text, uname, bb, callback){
var id = getUrlVars()["id"];
var uname=$("#uname").val();
var bb=$("#bb").val();
   $.ajax({
      'url': 'post.php?uname="+uname"',
      'type': 'post',
      'dataType': 'json',
      'data': {
                'id': id,
                'mode': 'post',
                'user': user,
                'uname': uname,
                'bb': bb,
                'text': text
                 },
      'success': function(result){
                 callback(result);
                 },
      'error': function(e){
                 console.log(e);
                 }
                });
        };
};
var c = new Chatter();
$(document).ready(function(){
        $('#formPost').submit(function(e){
                e.preventDefault();
                var user = $('#postUsername');
                var text = $('#postText');
                var err = $('#postError');
                var id = getUrlVars()["id"];
var uname=$("#uname").val();
var bb=$("#bb").val();

 c.postMessage(user.val(), text.val(), function(result){
         if(result){
            text.val('');
         }
         err.html(result.output);
         });
         return false;
        });

在表单字段中,我将其设置为如下。

    <form action="post.php" method="post" id="formPost" name="local_storage_form" >

School post:<textarea id="postText" name="postText" class"postText"></textarea><br>

<input type="submit" >
</form>

不确定字段的 id 是什么,但您可以将其添加到成功函数中:

success: function(result){
               callback(result);
               $('#postText').val(''); // might need to change the id...
           },

jquery提供如果你想清除一个<input><textarea>标签使用.val('')或其<div>或任何使用.text('').html('')

在 Ajax 上 成功调用 只需重置表单

document.getElementById("#formPost").reset();

参考:重置表单