Can someone help me to convert these lines into one for loop?
var optie1 = "";
if($('#form #optie1_check').is(':checked')) {
optie1 = $('#form #optie1_naam').val();
}
var optie2 = "";
if($('#form #optie2_check').is(':checked')) {
optie2 = $('#form #optie2_naam').val();
}
I can’t seem to find my answer on Google, don’t really know how to search for it.
I want something like this:
for loop (i) {
var str = "optie" + (i+1) + "_check";
var str2 = "optie" + (i+1) + "_naam";
if($('#form #str').is(':checked')) {
opties = $('#form #str2').val();
}
}
And opties is an array I will be putting all the values in, so I can pass it later on with:
$.ajax({
type: "POST",
url: "voegOptiesToe.php",
data: "opties="+opties,
...
}
you can simply use
.serialize()which encode a set of form elements as a string for submissionhttp://api.jquery.com/serialize/
For example,