我有一個HTML表單,其中包含3組(A,B和C)中的一系列問題,用戶只能回答任一組的問題。我有一個腳本驗證他們的輸入,以檢查他們是否只回答了一個組中的所有問題。使用Javascript更新HTML表單中的隱藏輸入
我想捕捉他們回答的哪組問題,並將其作爲隱藏輸入發送。我設置一個隱藏的輸入是這樣的:
<input type="hidden" name="type" value="" id="type">
和理想,我想替換這個上提交枝條一樣的東西:
<input type="hidden" name="type" value="groupA" id="type">
我設置顯示窗體/腳本小提琴:
http://jsfiddle.net/fmdataweb/CGp8X/
,這裏是我當前的驗證腳本:
$('#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;
}
是否可以通過某種方式擴展它以捕獲他們回答的哪組問題? HTML表格有3個標籤來標識每個組:
<tbody class="groupA">