I've the following markup.
<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>
Similarly for all other days of the week. And in the Vue instance:
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);
}
}
}
});
But this seems not working. What I'm trying to do here is when I click on a button, I want to add all checked days and time in to a variable selectedDays
and add it to a hidden input field (json serialize the data).But what I've tried is not working well.
Can anybody direct me to the right direction? I've spent 3 days and I couldn't figure out how to fix this.
I've the following markup.
<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>
Similarly for all other days of the week. And in the Vue instance:
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);
}
}
}
});
But this seems not working. What I'm trying to do here is when I click on a button, I want to add all checked days and time in to a variable selectedDays
and add it to a hidden input field (json serialize the data).But what I've tried is not working well.
Can anybody direct me to the right direction? I've spent 3 days and I couldn't figure out how to fix this.
Share Improve this question asked Feb 27, 2016 at 3:11 user1012181user1012181 8,72611 gold badges72 silver badges110 bronze badges3 Answers
Reset to default 3Have you tried:
//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>
and for js:
new Vue({
el: '#example-3',
data: {
checkedNames: []
}
})
This is straight from the documentation. it will push all selected checkbox's value into the chackedNames array
Try using a puted variable. In your vue ponent:
puted:{
selectedDays:function(){
var days = [];
if(mon.selected)
days.push('mon');
if(tues.selected)
days.push('tues')
//do for each day
return days;
}
}
Now every time you access the selectedDays
variable, it will be automatically updated. No need to check it on-submit, just v-model
it to your hidden input, or submit the selectedDays
variable directly using ajax
not sure what weekends
or weekdays
are for but here is a working example
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'},
},
puted: {
selectedDays: function() {
return this.days.filter(function(item) {
return item.selected;
});
}
}
});
<script src="https://unpkg./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>
more examples
http://codepen.io/ctf0/pen/LbKYjg
http://codepen.io/ctf0/pen/JbQEZa
http://codepen.io/ctf0/pen/GNbrGK