I am trying to build array from checked checkbox values in views. When form submitting checked checkbox values will form a array. I have a checkbox fields like this. Please anyone help me.
{{Form::checkbox('additional_rooms[]', 'Prayer Room') }} Prayer Room
{{ Form::checkbox('additional_rooms[]', 'Study Room') }} Study Room
{{ Form::checkbox('additional_rooms[]', 'Store Room') }} Store Room
{{ Form::checkbox('additional_rooms[]', 'Servent Room') }} Servent Room
var values = [];
$('[name="additional_rooms[]"]:checked') // Select checked checkboxes
.each(function(index, element) { // Iterate each one
values.push($(element).val()); // Put its value in an array
})
console.log(JSON.stringify(values)); // JSON encode the array
You cat get all checked checkbox values in a variable after submit form. Example:
$("#form").submit(function(){
var values = [];
$('[name="additional_rooms[]"]:checked') // Select checked checkboxes
.each(function(index, element) { // Iterate each one
values.push($(element).val()); // Put its value in an array
});
console.log(JSON.stringify(values)); // JSON encode the array
});