【问题标题】:How do I change HTML Label Text Once File Has Been Selected using Javascript使用 Javascript 选择文件后如何更改 HTML 标签文本
【发布时间】:2016-08-08 14:34:47
【问题描述】:

感谢您查看我的问题。我有一个下面的表格,它有一个文件输入字段。可以肯定地说我已经隐藏了这个输入字段,现在我使用标签作为主要的“按钮”来选择文件。一旦用户单击“上传图片...”标签,我希望标签内的文本从“上传图片...”更改为文件名。我正在关注下面的本教程,但是编写的 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

【问题讨论】:

  • 为我工作。也许你有重复的 id profilepic?
  • 检查上面的编辑。

标签: javascript html


【解决方案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>

【讨论】:

  • 你能把 javascript 注释掉,让我看看发生了什么吗?我不太了解 javascript,但如果我知道该 javascript 代码的作用,我可以了解更多。 :D 感谢您的回复。我很快就会对此进行测试。从手机发送
  • 你能只显示名称而不是文件路径吗?
  • 我正在使用 Google Chrome 最新版本公开发布的版本。感谢您评论这些事情。这真的很有帮助。我绝对喜欢我如何从 stackoverflow 中学习,而不仅仅是获得答案。非常感谢!
  • 编辑:我是个白痴,使用
  • 很遗憾,代码仍然无法正常工作,请您看看这个新问题并在有时间的情况下回复您吗? stackoverflow.com/questions/38837056/…
【解决方案2】:

可以使用change事件,选择for属性值等于event.target的元素:#profilepic元素id,设置被选元素的.innerHTMLevent.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>

【讨论】:

  • 非常感谢您的回答。我会将您的两个答案结合在一起,以得到只是名称和图像。
猜你喜欢
  • 2022-01-18
  • 2015-03-13
  • 1970-01-01
  • 2011-05-28
  • 1970-01-01
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
相关资源
最近更新 更多