I have an HTML form with a series of questions in 3 groups (A, B and C) and users can only answer the questions from either group. I have a script that validates their input to check that they have answered all the questions in one group only.
I would like to capture which group of questions they answered and send that as a hidden input. I've setup a hidden input like this:
<input type="hidden" name="type" value="" id="type">
and ideally I would like to replace this on submit withe something like:
<input type="hidden" name="type" value="groupA" id="type">
I've setup a fiddle showing the form/script:
http://jsfiddle.net/fmdataweb/CGp8X/
and here's my current validation script:
$('#editRecord').on('submit', validate);
$('input.sectionclear').on('click', function() {
$(this).next().find('input').attr('checked', false);
});
function validate(ev) {
var sectionsTouched = $('tbody').has(':checked'),
inputs = {},
errorMessage = '';
if (sectionsTouched.length === 0) {
errorMessage = 'Please check something before submitting.';
} else if (sectionsTouched.length > 1) {
errorMessage = 'Please complete A or B or C only';
} else {
sectionsTouched.find('input').each(function(i, e) {
var me = $(e), name = me.attr('name');
inputs[name] = !!inputs[name] || me.is(':checked');
});
$.each(inputs, function(k, v) {
if (!v) {
errorMessage = 'It appears you have not completed all questions.';
}
return v;
});
}
if (errorMessage !== '') {
$('#error').html(errorMessage);
ev.preventDefault();
return false;
}
return true;
}
function seriesNames() {
var names = [];
$('h3').each(function(i, e) {
names.push($(e).text());
});
return names;
}
function namesCommaDelimited(namesArr, conjunction) {
var retval = '', i, l = namesArr.length - 1, delimiter = '';
if (l === 0) {
return retval;
}
for (i = 0; i < l; i += 1) {
retval += delimiter;
retval += namesArr[i];
delimiter = ', ';
}
retval += delimiter;
retval += conjunction;
retval += ' ';
retval += namesArr[l];
return retval;
}
Is it possible to extend this in some way to capture which group of questions they answered? The HTML table has 3 tags to identify each of the groups:
<tbody class="groupA">
