Javascript 是否有“提交后”jQuery 选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5169471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:55:15  来源:igfitidea点击:

Is there an "after submit" jQuery option?

javascriptjqueryformsform-submit

提问by polyhedron

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?

提交后如何清除?

采纳答案by lonesomeday

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
});

回答by Gondrup

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 的解决方案对我有用,但对于谷歌浏览器,我发现它仍然会提交空表单数据,除非我添加了这样的超时:

$('#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);
});

回答by Martin Jespersen

You could do something like this:

你可以这样做:

$('#imageaddform').submit(function(){
  setTimeout(function() {
    $('#imagefile').val('');
  },100);
});

回答by samarjit samanta

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?

我错过了这个问题吗?