简体   繁体   English

触发html文件上传文本字段?

[英]Triggering an html file upload text field?

I've stumbled across some code that works in Firefox but not Chrome. 我偶然发现了一些可在Firefox中运行的代码,但不适用于Chrome。

What seems to be happening is that there is a file upload textfield that is sized in CSS. 似乎正在发生的事情是,有一个文件上载文本字段的大小在CSS中。 There is also an Upload button immediately to its right for the user to click to open a dialog to select a file. 右侧还有一个“上传”按钮,供用户单击以打开一个对话框来选择文件。 The textfield length is 60, which I think is used to overlay the "Upload" button and not size the field. 文本字段的长度为60,我认为该字段用于覆盖“上传”按钮,而不用于调整字段大小。 I've reduced 60 to 50 in Firebug and it stops working. 我将Firebug的数量减少了60到50,并且停止工作了。

How can I invoke the file upload dialog from the Upload button? 如何从“上传”按钮调用文件上传对话框? I think I need to send some sort of click event to the textfield. 我想我需要向文本字段发送某种点击事件。

File: __________________ Upload   | Clicking field or Button opens file dialog

I've extracted the HTML: 我提取了HTML:

<input name="fileField" id="fileField" type="file" size="60">
<span><input type="text" id="fileUploadField"><span></span></span>
<a href="javascript://" class="button" id="uploadButton"><span class="action">Upload</span></a>

The following should work for you: 以下应该为您工作:

document.getElementById('uploadButton').addEventListener('click', function(){
    document.getElementById('fileField').click();

    return false;
}, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM