Javascript 提交后重置HTML表单
Javascript 提交后重置HTML表单,javascript,html,Javascript,Html,const scriptURL='URL' const form=document.forms['myform'] form.addEventListener('submit',e=>{ e、 预防默认值() 获取(脚本URL{ 方法:“POST”, 正文:新表单数据(表单) }) .then(response=>console.log('Success!',response)) .catch(error=>console.error('error!',error.message)) });
const scriptURL='URL'
const form=document.forms['myform']
form.addEventListener('submit',e=>{
e、 预防默认值()
获取(脚本URL{
方法:“POST”,
正文:新表单数据(表单)
})
.then(response=>console.log('Success!',response))
.catch(error=>console.error('error!',error.message))
});代码>
电子邮件
订阅
您需要获取实际表单,而不是jQuery对象:
$('#myform').get(0).reset();
为了让javascript知道要重置哪个表单,您需要向表单添加id,而不仅仅是名称。试试看
<form name="myform" id="myform">
试试这个:
$('#myform').find("input:not([type="submit"), textarea").val("");
它将清除表单中的所有数据。但如果您有预填充的值,则应使用以下代码:document.getElementById('myform').reset()
注意:使用“myform”作为表单id属性。例如:
清除表单的一种方法是将值设置为“空”,如下所示:
那么表单输入就没有价值了
$('#myBtn')。单击(函数(){
$('#myForm').val('');
});代码>
myBtn
获取真正的表单$('#myForm')[0].reset()
您需要在表单中添加id标记
如果您只是使用普通js,那么可以使用document.getElementById('myForm').reset()
,但仍然可以在表单中添加一个id标记
编辑:注释中指出的更正请注意,表单重置将使其恢复到呈现时的原始状态。如果预先填充了字段,那么这些字段将再次获得该值。除此之外,还有两件事需要解决:
$(“#myform”)
需要HTML中的id=“myform”
将表单作为jQuery对象获取李>
reset()
仅适用于HTML表单对象,而不适用于jQuery对象。将调用更改为$('#myform')[0].reset()
或document.getElementById('myform').reset()
document.getElementById(“myform”).addEventListener(“提交”,重设表单);
功能重置表格(事件){
//你的密码在这里
document.getElementById(“myform”).reset();
}
电子邮件
订阅
由于表单
无论如何都是在侦听器中声明和恢复的,因此可以对其调用重置
<html>
<head>
<script>
window.onload = function(){
const scriptURL = 'URL';
const form = document.forms['myform'];
form.addEventListener('submit', e => {
e.preventDefault()
fetch(scriptURL, {
method: 'POST',
body: new FormData(form)
})
.then(response => console.log('Success!', response))
.catch(error => console.error('Error!', error.message));
form.reset() //Reset the form
})
}
</script>
</head>
<body>
<form name="myform">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label for="email" class="sr-only">Email</label>
<input name="email" class="form-control" type="email" placeholder="Email" required>
</div>
</div>
<div class="col-md-6 col-sm-6">
<button type="submit" class="btn btn-default btn-block">Subscribe</button>
</div>
</form>
</body>
</html>
window.onload=函数(){
const scriptURL='URL';
const form=document.forms['myform'];
form.addEventListener('submit',e=>{
e、 预防默认值()
获取(脚本URL{
方法:“POST”,
正文:新表单数据(表单)
})
.then(response=>console.log('Success!',response))
.catch(error=>console.error('error!',error.message));
reset()//重置表单
})
}
电子邮件
订阅
你就快到了getElementByID
这样就可以在(“#myform”)中省略#
如果您使用document.getElementById('myform').reset(),这将起作用代码>
由于某些原因,reset()
不能仅对纯JS使用jquery。jquery不是所有东西都需要的
<form action='server-script.cgi' onsubmit='this.submit();this.reset();return false;'>
<blah>...</blah>
</form>
...
试试这个:
$('#yourInputID').val('');
这将按其Id重置所选输入。原因
对数据使用fetch()时,数据已经传递,您只能从服务器返回响应对象。fetch.then()中的reset()将不会执行
解决方法
使事情正常工作的一种方法是,不要在fetch中获取表单数据,而应该在获取数据之前收集数据&将其保存在某个地方并重置表单
代码
form.addEventListener('submit', e => {
e.preventDefault()
const options = {
method: 'POST',
body: new FormData(form),
}
form.reset();
fetch(scriptURL, options)
.then(response => console.log('Success!', response))
.catch(error => console.error('Error!', error.message))
});
请提供HTML表单代码!可能重复:可能重复的jQuery不够。@HarishST刚刚添加,请检查更新的问题,即使您的id选择器在我看来是错误的。还可以使用.get(0)。如果你真的想使用jquery,至少要保持一致。你能详细说明一下你的意思吗?他们没有说event;-)。您的jQuery语句不完全正确。选择器中缺少#
,您应该调用reset()
作为函数。使用[0]
而不是get(0)
没有错:清空所有值真的与重置表单一样吗?那么预填充值呢?@Delowar刚刚尝试了你的建议,似乎是在重置整个页面而不是表单如果你有预填充值,你应该使用以下代码:document.getElementById('myform').reset();
$('#yourInputID').val('');
form.addEventListener('submit', e => {
e.preventDefault()
const options = {
method: 'POST',
body: new FormData(form),
}
form.reset();
fetch(scriptURL, options)
.then(response => console.log('Success!', response))
.catch(error => console.error('Error!', error.message))
});