<input type="file" value="Browse" name="avatar" id="id_avatar" />
我尝试修改value
,但它没有起作用。如何自定义按钮文本?
<script type="text/javascript" src="js/bootstrap-filestyle.min.js"> </script>
通过 JavaScript:
$(":file").filestyle();
通过数据属性:
<input type="file" class="filestyle" data-classButton="btn btn-primary" data-input="false" data-classIcon="icon-plus" data-buttonText="Your label here.">
隐藏文件选择框。创建一个新的输入框来捕捉点击事件并将其转发到隐藏的输入框:
<input type="button" id="loadFileXml" value="loadXml" onclick="document.getElementById('file').click();" />
<input type="file" style="display:none;" id="file" name="file"/>
;
。 - ATekuploadImgFile.change
处理程序,实际上我最终使用带有设置为文件输入ID的for属性的标签元素。 - asparism<img src="/images/uploadButton.png" id="upfile1" style="cursor:pointer" />
<input type="file" id="file1" name="file1" style="display:none" />
JQuery:
$("#upfile1").click(function () {
$("#file1").trigger('click');
});
注意:在IE9和IE10中,如果您通过JavaScript触发文件输入的onClick事件,则表单将被标记为“危险”,并且无法使用JavaScript提交,不确定是否可以传统提交。
<input type="file"
在 asp.net gridview 行内,这些行可以动态添加.. 因此,在点击图像时调用相应的输入点击触发器(同一行)将是一个真正的头痛! :/ - sohaiby“上传文件…”文本是由浏览器预定义的,无法更改。 唯一的解决方法是使用基于Flash或Java的上传组件,例如swfupload。
在所有浏览器上完美运作,希望它对你也有效。
HTML:
<input type="file" class="custom-file-input">
CSS:
.custom-file-input::-webkit-file-upload-button {
visibility: hidden;
}
.custom-file-input::before {
content: 'Select some files';
display: inline-block;
background: -webkit-linear-gradient(top, #f9f9f9, #e3e3e3);
border: 1px solid #999;
border-radius: 3px;
padding: 5px 8px;
outline: none;
white-space: nowrap;
-webkit-user-select: none;
cursor: pointer;
text-shadow: 1px 1px #fff;
font-weight: 700;
font-size: 10pt;
}
.custom-file-input:hover::before {
border-color: black;
}
.custom-file-input:active::before {
background: -webkit-linear-gradient(top, #e3e3e3, #f9f9f9);
}
将 content: '选择一些文件';
替换为你想在 ''
内显示的文本。
如果在 Firefox 中无法正常工作,请改用以下方法替代 input:
<label class="custom-file-input" for="Upload" >
</label>
<input id="Upload" type="file" multiple="multiple" name="_photos" accept="image/*" style="visibility: hidden">
<label class="btn btn-primary">
<i class="fa fa-image"></i> Your text here<input type="file" style="display: none;" name="image">
</label>
[使用代码片段进行编辑]
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<label class="btn btn-primary">
<i class="fa fa-image"></i> Your text here<input type="file" style="display: none;" name="image">
</label>
我认为这正是您所需要的:
<button style="display:block;width:120px; height:30px;" onclick="document.getElementById('getFile').click()">Your text here</button>
<input type='file' id="getFile" style="display:none">
这个是一个JQuery插件,用于更改输入文件元素的按钮文本。
示例HTML:
<input type="file" id="choose-file" />
JS示例:
$('#choose-file').inputFileText({
text: 'Select File'
});
结果:
<input id="uploadFile" placeholder="Choose File" disabled="disabled" />
<div class="fileUpload btn btn-primary">
<span>Your name</span>
<input id="uploadBtn" type="file" class="upload" />
</div>
JavaScript
document.getElementById("uploadBtn").onchange = function () {
document.getElementById("uploadFile").value = this.value;
};
更多 http://geniuscarrier.com/how-to-style-a-html-file-upload-button-in-pure-css/
我知道,虽然没有人要求,但如果有人在使用Bootstrap,可以通过标签和CSS伪选择器来更改它。
要更改按钮文本:
.custom-file-label::after {
content: "What's up?";
}
更改字段文本:
<label class="custom-file-label" for="upload">Drop it like it's hot</label>
这里是一个fiddle示例。
::file-selector-button
这样的伪元素。也许可以以某种方式使用它们。 - Sebastian Simon