[Vuejs]-Vue js – Add text and dropdown box inside the html content editable box

0👍

Vuejs is data-driven, MVVM thinks, if you want to create multiple ‘input
‘ tag Please use v-for more reasonable instead of dynamically creating DOM

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <style scoped>
        .content-editable {
            border: 1px solid #ccc;
            padding: 10px;
            min-height: 100px;
            margin-bottom: 10px;
        }
    </style>
</head>

<body>
    <div id="app">
        <template>
            <div>
                <button @click="addDropdown">creatSelect</button>
                <div v-for="(dropdown, index) in dropdowns" :key="index">
                    <select v-model="dropdown.selectedValue">
                        <option v-for="option in dropdown.options" :value="option.value" :key="option.value">{{
                            option.label }}</option>
                    </select>
                </div>
            </div>
            <button @click="getAllSelectedValues">getValue:</button>
            <div>valueList:{{ allSelectedValues }}</div>
        </template>

    </div>



</body>

</html>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            dropdowns: [],

            dropdownOptions: [
                { value: 'option1', label: 'option1' },
                { value: 'option2', label: 'option2' },
                { value: 'option3', label: 'option3' },
                { value: 'option4', label: 'option4' },
            ],
            allSelectedValues: [],
        },
        methods: {
            addDropdown() {
                this.dropdowns.push({
                    selectedValue: null,
                    options: this.dropdownOptions,
                });
            },
            getAllSelectedValues() {
                this.allSelectedValues = this.dropdowns.map((dropdown) => dropdown.selectedValue);
            },
        },

    })


</script>

希望能帮到你!

Leave a comment