screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <h3>Vue get text of selected option</h3> <select id="my-select" v-model="selectedOption"> <option disabled value="">Please select one</option> <option v-for="option in options" :key="option.value" :value="option.value">{{ option.text }}</option> </select> <p v-if="selectedOptionText">The selected option is: {{ selectedOptionText }}</p> </div> <script type="module"> const app = new Vue({ el: "#app", data() { return { selectedOption: '', options: [ { value: '1', text: 'Option 1' }, { value: '2', text: 'Option 2' }, { value: '3', text: 'Option 3' }, ], selectedOptionText: '', }; }, watch: { selectedOption(newVal) { // Find the selected option based on its value const selectedOption = this.options.find( option => option.value === newVal ); // Update the selected option's text this.selectedOptionText = selectedOption ? selectedOption.text : ''; }, }, }); </script> <style scoped> /* Style the select element */ select { font-size: 16px; padding: 10px; border: 1px solid #ccc; border-radius: 5px; width: 200px; } /* Style the disabled option */ option[disabled] { color: #ccc; } /* Style the selected option */ option[selected] { background-color: #007bff; color: #fff; } /* Style the paragraph element */ p { font-size: 16px; margin-top: 10px; } </style> </body> </html>