在全局服务器中提交ajax后清除表单值

时间:2016-08-19 12:54:14

标签: javascript php jquery ajax forms

这是我的表格:

<form class="form-horizontal" role="form" name="myForm" method="POST" action="http://abcd.xyz/server/post.php">
            <div class="form-group">
                <label class=" col-sm-2 " for="name">NAME:</label>
                <div class="col-sm-10 ">
                    <input type="name" class="form-control" name="fname" placeholder="Enter name" required="name" >
                </div>
            </div>
            <div class="form-group">
                <label class=" col-sm-2 " for="number">EXPENSIVE:</label>
                <div class="col-sm-10 ">
                    <input type="number" class="form-control" name="exp" placeholder="Enter Number" required="number" >
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 " for="date">DATE:</label>
                <div class="col-sm-10 ">
                    <input type="date" class="form-control" name="date" placeholder="Enter date" required="date" >
                </div>
            </div>
            <div class="form-group">
                <label for="comment" class="col-sm-2 ">COMMENTS:</label>
                <div class="col-sm-10 ">
                    <textarea class="form-control" rows="5" name="comment" placeholder="Enter Comments" required="text" ></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-10 ">
                    <button type="submit" id="submit" name="update" class="btn btn-default" >Submit</button>
                </div>
            </div>
        </form>

以下是处理我的表单的ajax标记:

$('form.form-horizontal').on('submit',function(){
var that =$(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};

that.find('[name]').each(function(index,value){
    var that = $(this),
    name = that.attr('name'),
    value = that.val();

    data[name] = value;
});
$.form-horizontal({
    url:"http://abcd.xyz/server/post.php",
    type: type,
    data: data,
    success: function(response){
     console.log(response);
     $('form.form-horizontal')[0].reset();//   
    }
});
return false; });

上述ajax代码不能在全局服务器上运行,但它位于本地服务器上。

这是我脚本的success部分的更改版本:

success: function(response){
     console.log(response);
     $('form.ajax')[0].reset();//.1  
      $('form.ajax').val("");//  .2
     $('.ajax').val("");//       .3
    }

我试过了两个,但我仍然无法清除我的表格。

1 个答案:

答案 0 :(得分:0)

确保ajax请求不是跨域。http://abcd.xyz是您的服务器地址?