vue + element + select to get the label and value of the drop-down box (for example, the binding is id, but the background requires that both the id and the name corresponding to the id be passed)

vue + element + select 获取下拉框的 label 和 value 值( 例如 绑定的是 id , 但后台要求即传 id ,又要传 id 对应的 name )
<template>
  <div>
    <el-form :inline="true" :model="form" ref="form" :rules="formRule">
      <el-form-item label="标题" prop="titleId">
        <el-select v-model="form.titleId" filterable placeholder="请选择标题" @change="changeTitle">
          <el-option v-for="item in titleList" :key="item.id" :label="item.titleName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { formRule} from "@/utils/valid";
export default {
  data() {
    return {
      form: {},
      formRule,  // 表单校验
      titleList: [],  // 标题列表
      title: {},  // 选中的标题
    }
  },
  methods: {
    // 调接口获取标题列表
    getTitle() {
      titleList().then(res => {
        if (res.code == 0) {
          this.titleList= res.data;
        } else {
          this.$message.error(res.message)
        }
      })
    },  
    changeTitle(id) {
      this.title= this.titleList.find(item => item.id == id)   // this.title 是通过选中的id去从标题列表里面查找得到的是选中的那一条数据的一个对象,包含选中的数据的 name、id 等 , 后面可以通过 this.title.name 来获取标题名称
    },

element select gets name based on id 

1. Why should we obtain  the Select(选择器) selected  attributes label such as  sum id and  name etc.:

The data passed from the background, I want it to be displayed in  the column, and I want to dynamically obtain  the equal  attribute value of Select 选择器the selected  label value   for later use;idname

Guess you like

Origin blog.csdn.net/u012118993/article/details/128319478