欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

VUE动态添加输入框

程序员文章站 2022-06-20 10:54:45
...

data:

machineData: {
    address: '',
    type: 'UI',
    port: '',
    system: 'Linux',
    source: '',
    username: '',
    password: '',
    authKey: false,
    browser: [{browser: '', version: ''}],
}

template:

<div v-for="(browser, index) in machineData.browser" :key="index"
                         style="display: flex;justify-content: space-between">
                        <el-form-item :prop="'browser.' + index + '.browser'"
                                      :rules="{required: true, message: '请选择浏览器', trigger: 'blur'}" label="浏览器">
                            <el-select placeholder="请选择浏览器" v-model="browser.browser" @change="addBrowser(index)">
                                <el-option label="Chrome" value="Chrome"></el-option>
                                <el-option label="Firefox" value="Firefox"></el-option>
                                <el-option label="Internet Explorer" value="Internet Explorer"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item :prop="'browser.' + index + '.version'"
                                      :rules="{required: true, message: '请输入浏览器版本', trigger: 'blur'}" label-width="0"
                                      style="margin-left: 5%">
                            <el-input placeholder="请输入浏览器版本" v-model="browser.version"
                                      @keyup.enter="addBrowser(index)"></el-input>
                        </el-form-item>
                        <el-button type="danger" @click.prevent="removeBrowser(browser)" style="height: 40px">删除
                        </el-button>
                    </div>

methods:

// 添加浏览器
 addBrowser(index) {
     if (this.machineData.browser.length === index + 1) {
         this.machineData.browser.push({
             browser: '',
             version: ''
         });
     }
 },
 // 删除浏览器
 removeBrowser(item) {
     const index = this.machineData.browser.indexOf(item)
     if (this.machineData.browser.length !== 1) {
         if (index !== -1) {
             this.machineData.browser.splice(index, 1)
         }
     }
 },
相关标签: VUE