触发输入文件单击不会显示文件上传对话框 [英] Triggering a input file click does not bring the file upload dialog
问题描述
我有以下HTML:
<form method="post" action="/link" style="position:absolute; left:-1000px;">
<input type="file" name="gameUpload" id="gameUpload" />
</form>
我正在尝试将HTML中的上传功能整合为一体.
I am trying to integrate the upload feature in HTML into unity.
这个想法是在统一引擎中单击按钮时,它将调用javascript函数,而js函数将触发对文件输入元素的单击.
The idea is when a button is clicked in unity engine, it will make a call to a javascript function and the js function will trigger the click on the file input element.
这在Firefox和IE10,IE9和IE8中都可以正常工作.但它不能在Chrome和Safari中使用.
This works perfectly fine in Firefox and IE10, IE9 and IE8. But it is not working in Chrome and Safari.
JS代码:
function uploadImage(){
jQuery("#gameUpload").trigger("click");
}
我尝试使用一些文本进行跨度",并尝试在单击文本时触发文件输入的单击,效果很好.
I tried having a "span" with some text and tried triggering the file input's click when that text is clicked, that worked fine.
此外,我还执行了以下操作来检查是否触发了点击:
Also, I did the following to check whether the click is triggered:
jQuery("#gameUpload").unbind("click").bind("click",function(){
alert("I am open");
});
显然,由于我收到了警报,但未打开文件对话框,因此触发了点击.
Apparently, the click is getting triggered because I am getting the alert but the file dialog is not getting opened.
推荐答案
使用jQuery,只需执行以下操作:
With jQuery, just do this:
$('#imageUpload').click();
// or get the HTMLElement
$('#imageUpload').get(0).click();
仅使用html(我们知道,带有for属性的标签会将焦点放在表单元素的默认操作上.)
Use html only (as we know, label with for attribute will focus the form element's default actions so.)
<label for="imageUpload">Upload File!</label>
<input id="imageUpload" type="file" style="display:none"/>
这篇关于触发输入文件单击不会显示文件上传对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!