简体   繁体   English

如何使链接充当文件输入

[英]How to make a link act as a file input

I have a link, 我有一个链接,

<a id="upload-file">Upload your photo</a>

And if clicked, I want it to act as an browse file input 如果单击,我希望它充当浏览文件输入

<input id="upload-file" type="file"/>

How would I accomplish this? 我将如何完成?

HTML 的HTML

<input id="upload" type="file"/>
<a href="" id="upload_link">Upload your photo</a>​

CSS 的CSS

#upload{
    display:none
}

JS JS

$(function(){
    $("#upload_link").on('click', function(e){
        e.preventDefault();
        $("#upload:hidden").trigger('click');
    });
});

​DEMO. 演示

following will solved the problem 以下将解决问题

html html

<input id="upload-file" type="file"/>
<a id="fileupload">Upload your photo</a>

css 的CSS

#upload-file{
    display: none;
}​

js js

$("#fileupload").click(function(){
    $("#upload-file").click();
});​

http://jsfiddle.net/WXBKj/ http://jsfiddle.net/WXBKj/

You can have a hidden <input> tag that you can then call $('#upload').click() on in order to simulate a click. 您可以具有一个隐藏的<input>标记,然后可以对其调用$('#upload').click()来模拟点击。

Or, you can have a hidden <input> tag that has an id, and then just add a label attribute to your link. 或者,您可以具有ID的隐藏<input>标记,然后仅将label属性添加到链接。

HTML Only 仅HTML

Here's a pretty simple answer that works with no CSS, Javascript/jQuery and doesn't rely on any framework infrastructure. 这是一个非常简单的答案,它不使用CSS,Javascript / jQuery,也不依赖任何框架基础结构。

<label>
  <input type="file" style="display: none;">
  <a>Upload Photo link</a>
</label>

or even simpler 甚至更简单

<label>
  <input type="file" style="display: none;">
  Upload Photo link
</label>

EDITED: 编辑:

See This Demo: http://jsfiddle.net/rathoreahsan/s6Mjt/ 观看此演示: http : //jsfiddle.net/rathoreahsan/s6Mjt/

JQUERY: JQUERY:

$("#upload").click(function(){
    $("#upload-file").trigger('click');
});

HTML 的HTML

<a href="javascript:void(0)" id="upload">Upload your photo</a>
<input id="upload-file" type="file"/>

CSS 的CSS

#upload-file {
    display:none;
}

OR 要么

You may use nice plugins like this: 您可以使用像这样的漂亮插件:

http://blueimp.github.com/jQuery-File-Upload/ http://blueimp.github.com/jQuery-File-Upload/

In Angular 6, you can do like this, 在Angular 6中,您可以这样做,

<li class="list-group-item active cursor-pointer" (click)="file.click()">
    <i class="fas fa-cloud-upload-alt"></i> Upload <input type="file" #file hidden />
  </li>

When you click li html tag, the input button's click is triggered." hidden " attribute makes invisible. 当您单击li html标记时,将触发输入按钮的单击。“ hidden ”属性将不可见。

You can do it without Javascript like this. 您无需使用Java脚本就可以做到这一点。 cross-browser solution with attribute for : 具有属性跨浏览器溶液for

DEMO 演示

HTML 的HTML

<label for="inputUpload" class="custom-file-upload">Upload your file</label>
<input id="inputUpload" type="file" />

CSS 的CSS

#inputUpload {
    display: none;
}

.custom-file-upload {
    cursor: pointer;
    font-size: inherit;
    color: #0c090d;
}

.custom-file-upload:hover {
    text-decoration: underline;
}

DEMO 演示

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

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