是否有"提交后"jQuery选项?

pol*_*ron 46 javascript forms jquery form-submit

我有一个上传文件并在页面上定位iframe的表单.当用户单击提交时,我希望文件内容"清除".

我试过这个

$('#imageaddform').submit(function(){
    $('#imagefile').val('');
});
Run Code Online (Sandbox Code Playgroud)

但是它会在提交之前清除表单,因此不会上传任何内容.

提交后如何清除?

lon*_*day 38

如果你没有绑定其他处理程序,你可以这样做:

$('#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
});
Run Code Online (Sandbox Code Playgroud)


小智 25

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);
});
Run Code Online (Sandbox Code Playgroud)

  • Gondrup ...非常感谢您为Chrome发布该解决方案......在我找到您的解决方案之前,我一直在想我的想法.Chrome在提交之前正在清除表单. (3认同)