在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ vue中怎么觸發(fā)隱藏input:file的change事件?

vue中怎么觸發(fā)隱藏input:file的change事件?

現(xiàn)在的場景是這樣的 我界面上有個圖片 還有個隱藏的input:file 然后我想點擊圖片觸發(fā)input:file的change事件 實現(xiàn)選擇圖片上傳 現(xiàn)在的問題就是 隱藏的input:file的change事件觸發(fā)不了 我在image的點擊事件里 就是觸發(fā)不了 還希望大神指導下
Ps:已使用過的方式

  • input:file 加個ref 通過this.$refs.inputref.change/click ×
  • 給input:file 加個id或者class 然后document.getElementById/document.querySelector('id or class').onchange= function(){} ×
回答
編輯回答
局外人

new一個事件出來,手動觸發(fā)試試看

2018年1月27日 08:02
編輯回答
久不遇

this.$refs._testFile.dispatchEvent(new MouseEvent('click')) 完美解決

2018年9月8日 18:14
編輯回答
礙你眼

我猜你是想要這么一個東西,如果是的話用css即可解決。
圖片描述

實現(xiàn)思路如下:
1.input設(shè)置為opacity:0;
2.img的z-index < input的z-index

圖中實現(xiàn)的部分代碼:
HTML部分

      <img v-if="item.data != ''" v-bind:src=" item.data || index " >
      <input type="file" :id=" item.name " @change="pushImg($event,index)" accept="image/jpeg,image/png,image/gif">

CSS部分

  .imgArea img{
    position: absolute;
    top: 0;
    left: 0;
    width: 50px;
    height: 50px;
  }
  .imgArea input{
    position: absolute;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
    z-index: 5;
  }
2017年4月24日 11:10
編輯回答
風清揚

按鈕觸發(fā),使用js獲取input 沒啥大毛病

<template>
  <div>
    <el-button :loading="loading" type="primary" @click="handleUpload">select excel file</el-button>
    <input id="excel-upload-input" type="file" accept=".xlsx, .xls" class="c-hide" @change="handkeFileChange">
  </div>
</template>
  methods: {
    handleUpload() {
      document.getElementById('excel-upload-input').click()
    },
    handkeFileChange(e) {
             //upload
    }
  }
2017年10月23日 06:20
編輯回答
有你在

通過label內(nèi)包img,將label的for屬性設(shè)置為input的id即可

2018年6月14日 23:00