我有以下标记。
<div class="checkbox">
<label>
<input class="weekdays" name="wednesday[]" v-model="wed.selected" id="wednesday" type="checkbox"> Wed
</label>
</div>
<div class="checkbox">
<label>
<input class="weekdays" name="thursday[]" v-model="thu.selected" id="thursday" type="checkbox"> Thu
</label>
</div>
同样适用于本周所有其他日子。在Vue实例中:
new Vue({
el: '#provider',
data: {
mon: {selected: false, day: 'Mondays'},
tue: {selected: false, day: 'Tuesdays'},
wed: {selected: false, day: 'Wednesdays'},
thu: {selected: false, day: 'Thursdays'},
fri: {selected: false, day: 'Fridays'},
sat: {selected: false, day: 'Saturdays'},
sun: {selected: false, day: 'Sundays'},
weekends: {selected: false, day: 'Weekends'},
weekdays: {selected: false, day: 'Weekdays'},
fromTime: '',
toTime: '',
selectedDays:[{
days : [],
from: '',
to:''
}]
},
methods: {
addAvailability: function() {
if(this.mon.selected)
{
var days = [];
days.push(this.mon.day);
this.selectedDays['days'].push(days);
}
}
}
});
但这似乎不起作用。我在这里尝试的是当我点击一个按钮时,我想将所有检查的日期和时间添加到变量selectedDays
并将其添加到隐藏的输入字段(json序列化数据)。但是我所尝试的并不是很好。
有人可以指引我走向正确的方向吗?我花了3天时间,我无法弄清楚如何解决这个问题。
答案 0 :(得分:3)
你试过了吗?
//HTML
<div id='example-3'>
<input type="checkbox" id="jack" value="Jack" v-model="checkedNames">
<label for="jack">Jack</label>
<input type="checkbox" id="john" value="John" v-model="checkedNames">
<label for="john">John</label>
<input type="checkbox" id="mike" value="Mike" v-model="checkedNames">
<label for="mike">Mike</label>
<br>
<span>Checked names: {{ checkedNames }}</span>
</div>
和js:
new Vue({
el: '#example-3',
data: {
checkedNames: []
}
})
这直接来自文档。它会将所有选中的复选框的值推送到chackedNames数组
答案 1 :(得分:2)
尝试使用计算变量。在您的vue组件中:
computed:{
selectedDays:function(){
var days = [];
if(mon.selected)
days.push('mon');
if(tues.selected)
days.push('tues')
//do for each day
return days;
}
}
现在每次访问selectedDays
变量时,它都会自动更新。无需检查提交,只需v-model
将其隐藏到您的隐藏输入,或直接使用ajax提交selectedDays
变量
答案 2 :(得分:0)
不确定weekends
或weekdays
是什么,但这是一个有效的例子
new Vue({
el: '#provider',
data: {
days: [
{name: 'Mondays', selected: false, from: '', to:''},
{name: 'Tuesdays', selected: false, from: '', to:''},
{name: 'Wednesdays', selected: false, from: '', to:''},
{name: 'Thursdays', selected: false, from: '', to:''},
{name: 'Fridays', selected: false, from: '', to:''},
{name: 'Saturdays', selected: false, from: '', to:''},
{name: 'Sundays', selected: false, from: '', to:''},
],
weekends: {selected: false, day: 'Weekends'},
weekdays: {selected: false, day: 'Weekdays'},
},
computed: {
selectedDays: function() {
return this.days.filter(function(item) {
return item.selected;
});
}
}
});
&#13;
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="provider">
<div v-for="day in days">
<label :for="day.name">
<input type="checkbox" name="days" :id="day.name" :value="day.name" :checked="day.selected" v-model="day.selected"> {{day.name}}
<input type="text" v-model="day.from">
<input type="text" v-model="day.to">
</label>
</div>
<hr>
{{selectedDays}}
</div>
&#13;
更多例子
http://codepen.io/ctf0/pen/LbKYjg