私の質問を見ていただきありがとうございます。Javascriptを使用してフォーム内でファイルを選択する際にラベルテキストを変更する方法
私は以下のリンク先でこれに似た質問をしました。残念ながら、答えはコードスニペットでは動作しますが、私のサイトでは動作しません。空白のHTML文書を作成し、次のコードを挿入しました。私がしようとしていることの基本はこれです。ファイル入力タグはCSSを使用して非表示になります。次に、ラベルタグを使用して入力タグの制御を引き継ぎます。ファイルを選択すると、ファイル名(パスではない)がラベル内に表示され、画像もユーザーに表示されるようにします。私の最後の質問で以前に述べたように、私はjQueryを使いたくありません。私のコードを見て、手伝ってくれてありがとう!
相続人は、私の前の質問:How do I change HTML Label Text Once File Has Been Selected using Javascript
相続人は私の「index2.php」ファイル内のコード:
<html>
<head>
<script>
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 */
</script>
</head>
<body>
<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="#" />
Upload Picture...
</label>
<br>
<div class="button-wrap">
<button>Change Picture</button>
</div>
</form>
</div>
</body>
</html>
... 'でをあなたの "ファイルのアップロード" ラップ'と 'innerHTML'を変更。 –
私はタグを使いたくありません。アドバイスをありがとうが、それは私がやろうとしているものではない。 –