on submit, i need to collect the values for a series of checkbox fields with a common name in array format. i tried doing something like:
$('form').submit(function(e){
$(':input:checkbox[name=fields[]]', this).each(function(){
console.log($(this).attr('name'));
console.log($(this).val);
});
});
but the “[name=fields[]]” doesn’t restrict the way i think it should.
what i eventually need to do is determine how many checkboxes have been checked from that fields array. i’m just sending to the console log while i figure out the conditions.
Edited:
http://jsfiddle.net/daybreaker/7UFs6/3
This will return a
fieldsarray with keys for eachfields[whatever]and a value equal to how many checkboxes are checked within eachfield[whatever].In my jsfiddle example, I use
fields[opt1]andfields[opt2], and though technically probably not a good idea at all to have a key with brackets in it, it works. If you want, you could do some regular expressions to pull out theopt1inside and make that the key.