我有这个HTML代码
<form method="post" id="upload_video" enctype="multipart/form-data" action="/Home/Save">
<div id="form_box">
<input type="text" name="name" id="name" value="" style="color: rgb(0, 0, 0);"><br>
<input type="text" name="email" id="email" value="" style="color: rgb(0, 0, 0);"><br>
<input type="text" name="mobile" id="mobile" value="Κινητό" style="color: rgb(0, 0, 0);"><br>
<input type="checkbox" id="checkbox">
</div>
<input type="file" name="file" id="upload_file" style="position:absolute;top:-100px;">
</form>
<div id="upload"></div>
我希望通过单击#upload div然后单击automaticallu = y select file,当我这样做时,然后提交我的表单。所以我有这个脚本
$('#upload').click(function () {
$('#upload_file').click();
});
$("#upload_file").change(function () {
$('#upload_video').submit();
});
$('#upload_file')。click()事件适用于所有浏览器,但$('#upload_file')。change()仅适用于FF和Chrome,但不适用于IE。 所以我的表单只在FF和Chrome中提交..
有什么想法吗?
答案 0 :(得分:1)
从这篇文章:jQuery change method on input type="file"
看起来你可能不得不这样做:
$("#upload_file").live( 'change', function () {
$('#upload_video').submit();
});
答案 1 :(得分:0)
刚刚发现由于安全原因,这在IE中是不可能的
答案 2 :(得分:0)
我不确定为什么Kory的答案被标记为答案,因为这不适用于Chrome或更新版本的IE。这是一种安全风险,因为文件可以在未经用户同意的情况下上传。
对于那些试图这样做的人,你有很多选择。上传多个文件有一些非常便宜的选项(uploadify或plupload是我用过的两个。)如果你只需要上传一个文件,那么你可以使用iframe上传图片(google上传文件使用iframe,有吨示例。)在iframe中,您需要在“onload”事件上调用javascript函数。上传图像后,您可以将一些json返回到iframe窗口,该窗口提供上载状态(成功/失败)。根据您要完成的操作,您可以将数据传回json,也可以将数据传回传递json中的重定向页面URL。我希望这会有所帮助。
〜干杯!!