javascript jQuery 自动上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4476546/
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-10-25 13:48:42  来源:igfitidea点击:

jQuery auto upload file

javascriptjquery

提问by Blur

I want the file input will automatic upload my image without enter any submit button.

我希望文件输入将自动上传我的图像而无需输入任何提交按钮。

<form action="/upload/" method="post" enctype="multipart/form-data">
  <input type="hidden" name="user_id" value="47" />
  <input type="file" name="upload" />
</form>

Let me know the trick with jQuery

让我知道 jQuery 的诀窍

回答by karim79

You can submit the form on the file input's onchange event, like this:

您可以在文件输入的 onchange 事件上提交表单,如下所示:

$("input[name=upload]").change(function() {
    $(this).closest("form").submit();
});

回答by lambinator

With more recent versions you can also just set the autoUpload option to true:

对于更新的版本,您还可以将 autoUpload 选项设置为 true:

$('#fileupload').fileupload({ autoUpload: true });