我如何改变HTML标签文本一旦文件已选择使用Javascript
How do I change HTML Label Text Once File Has Been Selected using Javascript
感谢观看我的问题。我有一个表单下面有一个文件输入字段。可以肯定地说,我已经隐藏了这个输入字段,现在使用标签作为选择文件的主要"按钮"。一旦用户点击"Upload Picture…"标签,我希望标签内的文本从"Upload Picture…"变为文件名。我在下面遵循这个教程,然而javascript编写(在教程中)是为多个文件能够被用户选择。我只允许我的用户选择一个文件。这是一个改变你的个人资料图片页面,让你对这个表单的作用有一点了解。
代码如下:
<!-- Change Picture Form -->
<div class="changepic-wrap">
<form action="changepicauth.php" method="post">
<input type="file" name="profilepic" id="profilepic" class="inputfile">
<br>
<label for="profilepic">
<img src="/images/profile/upload.png" />
Upload Picture...
</label>
<br>
<div class="button-wrap">
<button>Change Picture</button>
</div>
</form>
</div>
下面是教程:
http://tympanus.net/codrops/2015/09/15/styling-customizing-file-inputs-smart-way/谁能帮我解释一下我该怎么做?我在想一个事件监听器,但我不确定javascript。我是一个后端开发人员,对javascript知之甚少。我不希望使用JQUERY。只有javascript。再次感谢您的帮助StackOverflow成员!: D
编辑:有人说,文本已经在变化了。这不是……我希望改变LABEL文本。"文件输入"标签由两部分组成。按钮,以及按钮旁边的文本。我使用下面的SASS代码隐藏了输入标记。这样只会显示"上传图片…"标签文本。请确保将此SASS代码添加到您的文件中,以便您可以看到LABEL文本不会更改。
.inputfile
width: 0.1px
height: 0.1px
opacity: 0
overflow: hidden
position: absolute
z-index: -1
您可以使用javascript onchange
事件来检测#profilepic
input
的值何时发生变化
当它这样做时,您可以捕获#profilepic
input
的新value
,并用该value
替换标签的文本。
例子:
var profilePic = document.getElementById('profilepic'); /* finds the input */
function changeLabelText() {
var profilePicValue = profilePic.value; /* gets the filepath and filename from the input */
var fileNameStart = profilePicValue.lastIndexOf(''''); /* finds the end of the filepath */
profilePicValue = profilePicValue.substr(fileNameStart + 1); /* isolates the filename */
var profilePicLabelText = document.querySelector('label[for="profilepic"]').childNodes[2]; /* finds the label text */
if (profilePicValue !== '') {
profilePicLabelText.textContent = profilePicValue; /* changes the label text */
}
}
profilePic.addEventListener('change',changeLabelText,false); /* runs the function whenever the filename in the input is changed */
<div class="changepic-wrap">
<form action="changepicauth.php" method="post">
<input type="file" name="profilepic" id="profilepic" class="inputfile">
<br>
<label for="profilepic">
<img src="/images/profile/upload.png" />
Upload Picture...
</label>
<br>
<div class="button-wrap">
<button>Change Picture</button>
</div>
</form>
</div>
您可以使用change
事件,选择具有for
属性值等于event.target
的元素;#profilepic
元素id
,设置所选元素的.innerHTML
为event.target.files[0]
.name
document.getElementById("profilepic")
.addEventListener("change", function(e) {
document.querySelector("[for=" + e.target.id + "]")
.innerHTML = e.target.files[0].name;
})
.inputfile {
width: 0.1px;
height: 0.1px;
opacity: 0;
overflow: hidden;
position: absolute;
z-index: -1;
}
<!-- Change Picture Form -->
<div class="changepic-wrap">
<form action="changepicauth.php" method="post">
<input type="file" name="profilepic" id="profilepic" class="inputfile">
<br>
<label for="profilepic">
<img src="/images/profile/upload.png" />Upload Picture...
</label>
<br>
<div class="button-wrap">
<button>Change Picture</button>
</div>
</form>
</div>
相关文章:
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- java,javascript签名的小程序(文件选择,ftp上传)安全性/套接字异常
- 输入类型=“;文件“;onclick有效,但文件选择不显示
- 选择同一文件时未触发HTML输入文件选择事件
- 多个文件选择
- 当您选择不同的视图时,Google文件选择器示例会中断,例如google.picker.ViewId.DOCS
- JavaScript/HTML5音频:在Android Chrome中播放用户通过文件选择器加载的mp3文件
- 如何将文件选择器添加到angularjs模板中
- Angular文件上传插件:文件选择在Firefox中无法正常工作
- 从其他位置触发文件选择窗口
- IE8或IE9-支持多文件选择上传,无需Flash和HTML5
- 在windows8中,将“从文件选择器中拾取的文件”定义为Listview数据源
- 文件选择/文件API-将文件发送到嵌入式PDFObject
- 显示从文件选择器中选择的图像的图像缩略图
- 此api版本不支持onedrive文件选择器aadsts70001应用程序
- 在聚焦文本输入时触发文件选择
- Phonegap:解析从本机文件选择器获取 content:// URI
- HTML / JavaScript文件选择检查
- 如何在jquery中传递文件选择事件
- j查询文件选择与超链接