后端返回给我categoryId是17 我要从category:[{name:'张三',categoryId:17},{name:'张三',categoryId:18},{name:'张三',categoryId:19},{name:'张三',categoryId:20}]找到id是17的数据 把他的name渲染到 <el-form-item label="商品类目" prop="categoryId"> <el-select @focus="open" v-model="form.categoryId" filterable clearable placeholder="请选择商品类目" > <el-option v-for="item in category" :key="item.categoryId" :label="item.name" :value="item.categoryId" > </el-option> </el-select> </el-form-item>中的select 并且v-model绑定这个id 怎么做
时间: 2023-11-22 18:54:54 浏览: 179
vue中v-model对select的绑定操作
可以使用 JavaScript 的 `find` 方法来查找 `category` 数组中 `categoryId` 等于 17 的元素,然后将其 `name` 属性赋值给 `form.categoryName`,将其 `categoryId` 属性赋值给 `form.categoryId`。具体实现代码如下:
```javascript
const category = [{name:'张三',categoryId:17},{name:'张三',categoryId:18},{name:'张三',categoryId:19},{name:'张三',categoryId:20}];
const selectedCategory = category.find(item => item.categoryId === 17);
if (selectedCategory) {
form.categoryName = selectedCategory.name;
form.categoryId = selectedCategory.categoryId;
}
```
然后在页面中将 `v-model` 绑定到 `form.categoryId` 上即可:
```html
<el-form-item label="商品类目" prop="categoryId">
<el-select
@focus="open"
v-model="form.categoryId"
filterable
clearable
placeholder="请选择商品类目"
>
<el-option
v-for="item in category"
:key="item.categoryId"
:label="item.name"
:value="item.categoryId"
>
</el-option>
</el-select>
</el-form-item>
```
阅读全文