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

标签 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
    }

我尝试了这两种方法,但仍然无法清除我的表单。

最佳答案

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

关于javascript - 在全局服务器中提交ajax后清除表单值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39039588/

相关文章:

javascript - 箭头函数中的评估

javascript - 将数组映射到 API 调用 - 错误处理

javascript - 使用extjs显示同一模型的不同 View

javascript - 隐藏列表项并在单击时显示新的 li,但现在我总是需要一个空的 li 到 "hide"它们全部

使用 beanstalkd 进行 PHP 异步处理。你推荐吗?

php - 循环两倍 - 将 double 写入 MySQL 表

javascript - 函数内同名变量改变传递参数的值

javascript - 如何在完全加载后(循环)打开和关闭弹出窗口?

javascript - FullCalendar 在设定的时间内每周重复 mysql 事件

javascript - 防止 jQuery UI 中的重复数据可排序