v-for里获取input的值:
<!-- 个人 -->
<template>
<view>
<view class="person">
<view v-for="(item, index) in personMsg" :key="index">
<!-- 判断类型 -->
<template v-if="item.type == 'position'">
<view class="flex person_item">
<text>{{ item.name }}</text>
<view
class="downCheck flex"
@tap="chooseLocal($event, item.dataValue)"
>
<view
class="flex justify-center align-center"
style="width: 368rpx"
>
<view> {{ formData[item.dataValue] }}</view>
</view>
<u-icon name="map" style="margin-right: 22rpx"></u-icon>
</view>
</view>
</template>
<template v-else>
<!-- -->
<view class="flex align-center person_item">
<text>{{ item.name }}</text>
<input
class="downCheck"
@input="inputData($event, item.dataValue)"
/>
</view>
</template>
</view>
</view>
<button class="nextBtn" @tap="nextStep()">下一步</button>
</view>
</template>
<script>
export default {
data() {
return {
formData: {
nameValue: "", //输入框里的值
sexValue: "",
ageValue: "",
telValue: "",
potionValue: "",
idValue: "",
camaraPath: "",
idPicPath: "",
},
personMsg: [
{
name: "姓名",
icon: "none",
type: "input",
dataValue: "nameValue",
},
{
name: "性别",
icon: "none",
type: "input",
dataValue: "sexValue",
},
{
name: "年龄",
icon: "none",
type: "input",
dataValue: "ageValue",
},
{
name: "电话",
icon: "none",
type: "input",
dataValue: "telValue",
},
{
name: "公司地址",
icon: "none",
type: "position",
dataValue: "potionValue",
},
{
name: "身份证号",
icon: "none",
type: "input",
dataValue: "idValue",
},
],
};
},
methods: {
nextStep() {
//将数据传给父组件check
this.$emit("personData", this.formData);
},
inputData(event, dataValue) {
// var value = event.target.value;
this.formData[dataValue] = event.target.value;
},
},
};
</script>
<style lang='scss' scoped>
- 我的input是通过v-for遍历生成的,给@input根据配置项来传参的
就可以实现将每个input里的值存放在formData里面 - 核心代码:
@input="inputData($event, item.dataValue)"
inputData(event, dataValue) {
// var value = event.target.value;
this.formData[dataValue] = event.target.value;
},