gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:39:35 24 4
gpt4 key购买 nike

这是我的表格:

<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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com