1 回答

TA貢獻1856條經(jīng)驗 獲得超17個贊
item_index 始終為 0 的原因是因為您在圖像上設(shè)置的標簽鏈接到slider-item-file-1為每個文件輸入設(shè)置的 id。一個元素應(yīng)該有一個唯一的 id,在你的代碼中你沒有,所以當你點擊標簽時,它總是會訪問該 id 在正文中的第一次出現(xiàn),因此你總是會得到一個 0 的 item_index。如果你使 ids 動態(tài)這將解決您的問題。
這也是為什么您會注意到,當您單擊底部圖像時,它會跳到頁面頂部,因為它會轉(zhuǎn)到具有該 ID 的第一個文件輸入。
<label :for="'slider-item-file-' + parent_index + '-' + index + '-' + item_index">
<img v-if="item.preview.length>1" :src="item.preview" width="160px" height="200px"/>
<img v-else src="images/image6.png" width="160px" height="200px"/>
</label>
<input type="file" @change="onImageChange($event, parent_index, index, item_index)" :id="'slider-item-file-' + parent_index + '-' + index + '-' + item_index"/>
每個 id 將包含每個 for 循環(huán)的索引。
希望這可以幫助
添加回答
舉報