jQuery 在文件选择上提交表单不起作用
我试图在用户选择文件后立即启动文件上传。该表单应该消失,取而代之的是“正在上传您的图片...”消息。
到目前为止,我得到的只是隐藏的表单(和显示的消息),但上传没有发生。未提交表格。有什么想法吗?
这是 JS
<script>
$(function(){
$("#file_select").change(function(){
$(this).parents("#upload_form").submit();
$("#upload_form_div").hide();
$("#loading").show();
});
});
</script>
和 HTML
<div class="float_left" id="upload_form_div">
<h3>Select a picture for your profile (4 MB max):</h3>
<form action="http://example.com/profile/upload_picture"
id="upload_form"
enctype="multipart/form-data"
method="post" accept-charset="utf-8">
<input type="file" name="userfile"
id="file_select" />
<input type="hidden" name="upload" value="upload" />
<button id="submit" type="submit"><img height="24" width="24"
alt="Upload" src="images/icons/small/white/bended%20arrow%20right.png">
<span>Upload</span>
</button>
</form>
<form action="http://example.com/profile/remove_picture"
method="post" accept-charset="utf-8">
<button type="submit"><img height="24" width="24"
alt="Remove" src="images/icons/small/white/trashcan.png">
<span>Remove</span>
</button>
</form>
</div>
<div id="loading" style="display:none;">
Uploading your picture...
</div>
I am trying to initiate upload of a file as soon as the user selects the file. The form should disappear replaced by the "Uploading your picture..." message.
So far, all I get is the form being hidden (and message showing), but the upload is not happening. The form is not being submitted. Any ideas why?
This is the JS
<script>
$(function(){
$("#file_select").change(function(){
$(this).parents("#upload_form").submit();
$("#upload_form_div").hide();
$("#loading").show();
});
});
</script>
and the HTML
<div class="float_left" id="upload_form_div">
<h3>Select a picture for your profile (4 MB max):</h3>
<form action="http://example.com/profile/upload_picture"
id="upload_form"
enctype="multipart/form-data"
method="post" accept-charset="utf-8">
<input type="file" name="userfile"
id="file_select" />
<input type="hidden" name="upload" value="upload" />
<button id="submit" type="submit"><img height="24" width="24"
alt="Upload" src="images/icons/small/white/bended%20arrow%20right.png">
<span>Upload</span>
</button>
</form>
<form action="http://example.com/profile/remove_picture"
method="post" accept-charset="utf-8">
<button type="submit"><img height="24" width="24"
alt="Remove" src="images/icons/small/white/trashcan.png">
<span>Remove</span>
</button>
</form>
</div>
<div id="loading" style="display:none;">
Uploading your picture...
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它可能与这部分有关:
输入类型=文件不应该有值=属性(即使有,你也不应该把javascript放在那里!)
你还有一些表单中的javascript:
如果函数
validateFileExtension()
返回false
那么表单将不会提交。但是,您编写 jQuery 的方式意味着该消息仍然会出现。
编辑:
将提交按钮上的 ID 更改为“提交”以外的其他内容。
在 jQuery 文档中:
但是:
您应该考虑@Abdul 的解决方案,因为工作提交将使您远离您的消息。
如果您正在使用 CodeIgniter 并且不想使用 Ajax,则应考虑使用 flash 功能在表单提交后向用户显示消息。
It probably has something to do with this part:
An input type=file should not have a value= attribute (and even if it did, you shouldn't put javascript in there!)
You also have some javascript in the form:
If the function
validateFileExtension()
returnsfalse
then the form will not submit.However, the way you have written the jQuery means that the message will still appear.
EDIT:
Change the id on your submit button to something other than "submit".
In the jQuery docs:
HOWEVER:
You should consider @Abdul's solution, since the working submit will take you away from your message.
If you are using CodeIgniter and you don't want to use Ajax, you should consider using the flash functionality to display the message to the user after the form submits.
您应该考虑使用 jquery 表单插件 提交表单并 jquery 验证插件 用于验证表单的文件扩展名和所有内容。Jquery 表单插件使用 ajax 提交表单。这样您就不会被重定向到表单操作。还
如果你愿意,你可以考虑使用 jquery 对话框来显示结果。
You should think of using jquery form plugin to submit your form and jquery validate plugin to validate your form for file extensions and everything.Jquery form plugin submits your form using ajax. In that way you won't be redirected to form action. Also
if you want you can consider using jquery dialog to display the result.
提交文件选择表单并验证 CSV 文件输入
Submit form on file selection and validate for CSV file input