I have a form that uploads a file and targets an iframe on the page. When the user clicks submit, I want the file contents to "clear" out.
我有一个表单,用于上传文件并在页面上定位iframe。当用户单击提交时,我希望文件内容“清除”。
I tried this
我试过这个
$('#imageaddform').submit(function(){
$('#imagefile').val('');
});
But it clears the form before the submit, so nothing is ever uploaded.
但是它会在提交之前清除表单,因此不会上传任何内容。
Is how do I clear after submit?
提交后如何清除?
4 个解决方案
#1
33
If you have no other handlers bound, you could do something like this:
如果你没有绑定其他处理程序,你可以这样做:
$('#imageaddform').submit(function(e) {
e.preventDefault(); // don't submit multiple times
this.submit(); // use the native submit method of the form element
$('#imagefile').val(''); // blank the input
});
#2
24
Lonesomeday's solution worked for me but for Google Chrome I found it would still submit empty form data unless I added a timeout like this:
Lonesomeday的解决方案对我有用,但对于Google Chrome我发现它仍然会提交空表单数据,除非我添加了这样的超时:
$('#imageaddform').submit(function(e) {
e.preventDefault(); // don't submit multiple times
this.submit(); // use the native submit method of the form element
setTimeout(function(){ // Delay for Chrome
$('#imagefile').val(''); // blank the input
}, 100);
});
#3
4
You could do something like this:
你可以这样做:
$('#imageaddform').submit(function(){
setTimeout(function() {
$('#imagefile').val('');
},100);
});
#4
2
How are u submitting the form? if this is normal form post then then page wont exist in that case i am wondering if u are looking to clear the form before the page refreshses so that when the user comes back he doesn't see the values populated.
你是如何提交表格的?如果这是正常形式的帖子然后页面不会存在,在这种情况下,我想知道你是否想要在页面刷新之前清除表单,以便当用户回来时他没有看到填充的值。
If the form is submitted by ajax then you can
如果表单是由ajax提交的,那么你可以
function(){
$('form1')[0].submit();
clearForm();
}
Did i miss the question?
我错过了这个问题吗?
#1
33
If you have no other handlers bound, you could do something like this:
如果你没有绑定其他处理程序,你可以这样做:
$('#imageaddform').submit(function(e) {
e.preventDefault(); // don't submit multiple times
this.submit(); // use the native submit method of the form element
$('#imagefile').val(''); // blank the input
});
#2
24
Lonesomeday's solution worked for me but for Google Chrome I found it would still submit empty form data unless I added a timeout like this:
Lonesomeday的解决方案对我有用,但对于Google Chrome我发现它仍然会提交空表单数据,除非我添加了这样的超时:
$('#imageaddform').submit(function(e) {
e.preventDefault(); // don't submit multiple times
this.submit(); // use the native submit method of the form element
setTimeout(function(){ // Delay for Chrome
$('#imagefile').val(''); // blank the input
}, 100);
});
#3
4
You could do something like this:
你可以这样做:
$('#imageaddform').submit(function(){
setTimeout(function() {
$('#imagefile').val('');
},100);
});
#4
2
How are u submitting the form? if this is normal form post then then page wont exist in that case i am wondering if u are looking to clear the form before the page refreshses so that when the user comes back he doesn't see the values populated.
你是如何提交表格的?如果这是正常形式的帖子然后页面不会存在,在这种情况下,我想知道你是否想要在页面刷新之前清除表单,以便当用户回来时他没有看到填充的值。
If the form is submitted by ajax then you can
如果表单是由ajax提交的,那么你可以
function(){
$('form1')[0].submit();
clearForm();
}
Did i miss the question?
我错过了这个问题吗?