我可能一直盯著它看 - 問題與此類似。我向按鈕添加了一個事件偵聽器,但只有 onclick 方法會觸發某些內容。我想顯示上傳的所選檔案的名稱:
var ulButtons = document.getElementsByClassName("file-upload");
[...ulButtons].forEach(ulButton => {
ulButton.addEventListener("click", function() {
ulButton.value = null;
console.log("this works")
})
});
[...ulButtons].forEach(ulButton => {
ulButton.addEventListener("change", function() {
console.log("changed to:", this.value)
})
});
<label for="{field.id_for_label}" class="file-upload btn btn-secondary">upload file</label>
我不明白為什么單擊上傳按鈕總是顯示在控制臺中,但選擇檔案不會觸發changed to: ....
uj5u.com熱心網友回復:
如果您包含 type 的輸入欄位,您的代碼將起作用file。
var ulButtons = document.getElementsByClassName("file-upload");
[...ulButtons].forEach(ulButton => {
ulButton.addEventListener("click", function() {
ulButton.value = null;
console.log("this works")
})
});
[...ulButtons].forEach(ulButton => {
ulButton.addEventListener("change", function() {
console.log("changed to:", this.value)
})
});
<label for="{field.id_for_label}" class="file-upload btn btn-secondary">
<input type="file">upload file</input></label>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/402447.html
標籤:
