被禁用的 html 标签,无法响应事件

解决方案1:在父元素上添加事件

  • el-input 外添加 div
  • 在 div 上添加点击事件
    若对事件响应范围限制不严,可以在 el-form-item 上添加点击事件(点击表单标签时也会触发事件),必要时需加上事件修饰符 .native
  • el-input 添加样式 position: relative; z-index: -1,在原位置下降一层
    (若因其他样式影响,el-input 下降一层后无法显示,则可在 div 上添加 position: relative; z-index: 1 让父元素上升一层)
<script setup lang="ts">
import { ref } from "vue";

let form = ref({ name: "" });

function handleClick() {
  alert("点击了");
}
</script>

<template>
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="活动名称">
      <div @click="handleClick">
        <el-input
          style="position: relative; z-index: -1"
          v-model="form.name"
          disabled
        ></el-input>
      </div>
    </el-form-item>
  </el-form>
</template>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

解决方案2:改用 readonly

<script setup lang="ts">
import { ref } from "vue";

let form = ref({ name: "巴黎奥运会" });

function handleClick() {
  alert("点击了");
}
</script>

<template>
  <el-form :model="form" label-width="80px">
    <el-form-item label="活动名称">
      <el-input v-model="form.name" readonly @click="handleClick"></el-input>
    </el-form-item>
  </el-form>
</template>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.