我有兩個影像輸入欄位
<input type="file" accept="image/*" id="one" />
<input type="file" accept="image/*" id="two" />
我正在嘗試將值從 同步two到one,每當two收到輸入時,將值分配給one。two只是前端可見的一個欄位,one是用于資料收集和上傳的原始表單。
two.onchange = () => {one.value = two.value}
因為它是檔案欄位,所以我想知道這是否行不通(還沒有撰寫單元測驗,因為即使值是在前面記錄的,我懷疑檔案會在后端被捕獲)。如果有人提出一種切實可行的方法,我將不勝感激。
uj5u.com熱心網友回復:
您做錯了,您必須設定.files屬性,而不是.value因為瀏覽器將檔案存盤在files屬性中而不是value屬性中。你可以像下面的例子那樣做:
const one = document.getElementById('one');
const two = document.getElementById('two');
two.onchange = () => {one.files = two.files}
<input type="file" accept="image/*" id="one" />
<input type="file" accept="image/*" id="two" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/479512.html
標籤:javascript html 文件
