Heim > Fragen und Antworten > Hauptteil
P粉3215842632023-07-28 10:46:58
Vue.js是数据驱动的,采用MVVM思想。如果您想创建多个“input”标签,请使用v-for更合理,而不是动态创建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>
希望能帮到你!