vue 下拉框和输入框动态加减

<template>

  <div class="lin-container">

    <div class="lin-wrap-ui">

      <el-card class="box-card" style="margin-bottom:50px;padding-bottom:20px;">

        <div class="block-box">

          <el-row class="input-row" v-for="(item, index) in list" :key="index">

             <el-select class="el-select" v-model="item.label">

              <el-option

                v-for="(item, index) in addList"

                :key="index + 1"

                :label="item.label"

                :value="item.value"

              ></el-option>

            </el-select>

            <el-input v-model="item.text" placeholder="请输入内容" class="input-detail"></el-input>

            <div class="function">

              <i class="el-icon-remove" v-if="list.length-1" @click="removeContent(index)"></i>

              <i class="el-icon-circle-plus" v-if="index === list.length - 1" @click="addContent"></i>

            </div>

          </el-row>

          <el-button type="primary" @click="submit">主要按钮</el-button>

        </div>

      </el-card>

    </div>

  </div>

</template>

  list: [{ text: "", type: "" }],

            addList: [

                {

                    value: "老板",

                    label: "老板"

                },

                {

                    value: "经理",

                    label: "经理"

                }

            ]

 addContent() {

            this.list.push({text: ""});

        },

 

        removeContent(index) {

            this.list.splice(index, 1);

        },

        submit(){

            console.log(this.list)

        }

下面是一个简单的实现示例: ``` <template> <div class="form-group"> <label>{{ label }}</label> <div class="form-control"> <select v-model="selectedItem" @change="validateInput"> <option value="">请选择</option> <option v-for="item in options" :key="item.value" :value="item.value">{{ item.label }}</option> </select> <input v-model="inputValue" @input="validateInput" /> <div class="error-message">{{ errorMessage }}</div> </div> </div> </template> <script> export default { props: { label: { type: String, required: true }, options: { type: Array, required: true }, validate: { type: Function, required: true } }, data() { return { selectedItem: '', inputValue: '', errorMessage: '' } }, methods: { validateInput() { const isValid = this.validate(this.selectedItem, this.inputValue) this.errorMessage = isValid ? '' : '输入格式不正确' } } } </script> <style> .form-group { margin-bottom: 20px; } .form-control { display: flex; align-items: center; } .form-control select { margin-right: 5px; } .error-message { color: red; font-size: 12px; margin-top: 5px; } </style> ``` 在父组件中,你可以这样使用它: ``` <template> <div> <InputWithSelect label="姓名" :options="nameOptions" :validate="validateName" /> </div> </template> <script> import InputWithSelect from './InputWithSelect.vue' export default { components: { InputWithSelect }, data() { return { nameOptions: [ { label: '张三', value: 'zhangsan' }, { label: '李四', value: 'lisi' }, { label: '王五', value: 'wangwu' } ] } }, methods: { validateName(selectedItem, inputValue) { return /^[a-zA-Z]+$/.test(inputValue) } } } </script> ``` 如上代码所示,你需要传递一个选项数组、一个验证函数以及一个标签字符串给 `InputWithSelect` 组件。选项数组包含下拉框中的选项,验证函数接收两个参数(下拉框中的选项和输入框中的值),并返回一个布尔值表示是否通过验证。在子组件中,我们可以通过 `v-model` 指令来双向绑定下拉框输入框的值,并在 `validateInput` 方法中调用验证函数,更新错误信息。最后,我们在模板中显示错误信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值