用表格中我自己的按钮替换输入文件 [英] Replace input file with my own button in the form

查看:101
本文介绍了用表格中我自己的按钮替换输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想隐藏输入文件并使用按钮选择表单中的文件.
如果单击标签时使用<label for="input-file">Label</label>,则可以选择一个文件,但是我想成为一个按钮,或者至少要看起来像一个按钮.
这是JSFiddle中的代码示例:

Basicly I want to hide the input file and use a button to choose a file in the form.
If I use <label for="input-file">Label</label> when I click the label i get to choose a file, but I would like to be a button or at least to look like a button.
Here is a code sample in JSFiddle :

input[type=file] { display: none; }

<form>
    <input type="file" id="input-file">
    <label for="input-file">
        <button>Button</button>
    </label>
    <input type="submit">
</form>

如果我只是标签中的一个按钮,则它充当提交按钮,如果我指定其类型,则单击时不执行任何操作.

If I but a button in the label, it acts as a submit button, if I specify its type it does nothing on click.

是否可以使用我的按钮或按钮查找控件而不是使用input type="file"来上传表单中的文件?
它必须是HTML-CSS解决方案,不能使用JS或任何框架.

Is there any way to have upload a file in the form using my button or a button looking control instead of using the input type="file"?
It must be a HTML-CSS solution, no JS or any framework.

此CSS代码似乎使label看起来像一个标准按钮:

This CSS code appears to make the label look like a standard button :

label {
    appearance: button;
    -moz-appearance: button;
    -webkit-appearance: button;
    text-align: center;
    padding: 2px 6px 3px;
    border: 2px outset buttonface;
    font: 13.3333px Arial;
}

推荐答案

您将通过几行CSS实现此目的. 提琴

You'll achieve this with couple of lines of CSS. Fiddle

input[type="file"] {
    display: none;
}
.custom-file-upload {
    border: 1px solid #ccc;
    display: inline-block;
    padding: 6px 12px;
    cursor: pointer;
}

<label for="file-upload" class="custom-file-upload">
    <i class="fa fa-cloud-upload"></i> Custom Upload
</label>
<input id="file-upload" type="file"/>

这篇关于用表格中我自己的按钮替换输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆