我有一个呈现选择标记的组件。 select标签包含月份的十二个选项,每个选项都有value属性,此属性有一个数字,每个月从1到12,所以我想获取选项标签tha的这个value属性的值然后将此值与我组件的de data属性变量同步。我正在使用Vue JS。
<select v-model="month" class="form-control form-control-sm">
<option>Selecciona el mes</option>
<option value="1">Enero</option>
<option value="2">>Febrero</option>
<option value="3">>Marzo</option>
<option value="4">>Abril</option>
<option value="5">>Mayo</option>
<option value="6">>Junio</option>
<option value="7">>Julio</option>
<option value="8"> >Agosto</option>
<option value="9">>Septiembre</option>
<option value="10">>Octubre</option>
<option value="11">>Noviembre</option>
<option value="12">>Diciembre</option>
</select>
答案 0 :(得分:0)
您可以执行以下操作: https://jsfiddle.net/5k1fc9tj/
但是最好通过数据而不是通过html标记来做到这一点: https://jsfiddle.net/5k1fc9tj/1/
<div id="app">
You select {{ selectedMonthName }}
<br />
<select v-model="selectedMonth" class="form-control form-control-sm">
<option value="null">Selecciona el mes...</option>
<option v-for="month in monthes" :value="month.id">{{ month.name }}</option>
</select>
</div>
new Vue({
el: "#app",
data: {
selectedMonth: null,
monthes: [
{id: "1", name: "Enero" },
{id: "2", name: "Febrero" },
{id: "3", name: "Marzo" },
{id: "4", name: "Abril" },
{id: "5", name: "Mayo" },
{id: "6", name: "Junio" },
{id: "7", name: "Julio" },
{id: "8", name: "Agosto" },
{id: "9", name: "Septiembre" },
{id: "10", name: "Octubre" },
{id: "11", name: "Noviembre" },
{id: "12", name: "Diciembre" },
],
},
computed: {
selectedMonthName() {
for (let month of this.monthes) {
if (month.id === this.selectedMonth) {
return month.name;
}
}
return 'nothing :)'
},
},
});