Rumah > Soal Jawab > teks badan
P粉3215842632023-07-28 10:46:58
Vue.js dipacu data dan mengamalkan pemikiran MVVM. Jika anda ingin membuat berbilang teg "input", lebih masuk akal untuk menggunakan v-for dan bukannya mencipta DOM secara dinamik.
<!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>
Semoga ini membantu!