Reputation: 45
I want to push { "studentid": achecked[index].value }
this into a array, but if i push it my array is like this (2) [{…}, {…}]
and not my checkbox value.
this is my code:
var achecked = $("form input:checkbox:checked");
var values = [];
achecked.each(function (index) {
var student = { "studentid": achecked[index].value };
values.push(student);
});
console.log(values);
var postdata = {
'displayname': $("#displayname").val(),
'email': $("#email").val(),
'students': [
]
};
postdata.students = values;
<input type="checkbox" name="toevoegen" value="10" />
<button class="btn btn-success" id="aanmaken">Aanmaken</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
does someone knows and could help me?
Upvotes: 2
Views: 161
Reputation: 2646
Seems to be working for me. Not sure what your HTML looks like, but the selector form input:checkbox:checked
will require a form around all the checkboxes.
$('button').on('click', function(e) {
e.preventDefault();
var achecked = $("form input:checkbox:checked");
var values = [];
achecked.each(function (index) {
var student = { "studentid": achecked[index].value };
values.push(student);
});
console.log(values);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="checkbox" checked value="1" />
<input type="checkbox" checked value="2" />
<input type="checkbox" value="3" />
<input type="checkbox" checked value="4" />
<input type="checkbox" checked value="5" />
<button>Submit</button>
</form>
Upvotes: 1