【问题标题】:Adding form inputs dynamically with element-ui使用 element-ui 动态添加表单输入
【发布时间】:2020-02-07 02:08:58
【问题描述】:

我正在尝试通过单击添加字段的按钮和删除它们的按钮来动态添加字段,我希望两个字段出现在开头并且是必需的,并且可以选择添加更多并删除额外的那些。这样做的目的是为正在创建的问题添加多项选择问题的答案。

这是我目前的代码

表格

<div v-if="form.response_type_id === 2">
    <el-divider></el-divider>
    <div v-for="(option, index) in options" :key="index">
        <el-row>
            <el-col :span="22">
                <p class="el-form-item__label">Opciones</p>
            </el-col>
            <el-col :span="2">
                <div class="btn-link-plus action-button" @click="addOption(index)">
                    <i class="fas fa-plus" v-show="index == options.length - 1"></i>
                </div>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="20">
                <el-form-item prop="option">
                    <el-input v-model="option.option"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="2">
                <div class="btn-link-delete action-button" @click="removeOption(index)">
                    <i class="fas fa-trash-alt"
                        v-show="index || (!index && options.length > 1)"></i>
                </div>
            </el-col>
        </el-row>
</div>

数据

options: [];

方法

addOption() {
    this.options.push({
        survey_question_id: this.form.id,
        option: ''
    });
},
removeOption(index) {
    this.options.splice(index, 1);
},

目前,什么都没有出现,&lt;div v-for&gt; 中的所有内容都没有出现。代码背后的逻辑是,当问题的响应类型设置为多项选择时,上面的代码将出现在表单中,以允许用户创建不同的选择,其中两个是必需的。理想情况下,我只想将删除按钮添加到添加的输入而不是所需的输入。

【问题讨论】:

    标签: laravel vue.js element-ui


    【解决方案1】:

    你的解释有点误导。我注意到的是,您至少需要两个答案。 如果我理解正确

    removeOption(index) {
        if(this.options.length<=2) {
            alert('')
        } else {
            this.options.splice(index, 1);
        }
    },
    

    默认选择两个选项:

    data() {
        return {
            options: [{option1Obj}, {option2Obj}]
        }
    }
    

    【讨论】:

    • 有没有办法从一开始就从 2 个选项开始?
    • @Nancy 回答已编辑
    猜你喜欢
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 2017-08-14
    • 2016-07-30
    相关资源
    最近更新 更多