Is there an “after submit” jQuery option?

强颜欢笑 提交于 2019-11-26 16:12:03

问题


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.

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?


回答1:


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:


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:

$('#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:


You could do something like this:

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



回答4:


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

function(){
 $('form1')[0].submit();
 clearForm();
}

Did i miss the question?



来源:https://stackoverflow.com/questions/5169471/is-there-an-after-submit-jquery-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!