I have a very large form that's essentially a questionnaire.
It has 19 questions, some of which when answered Yes
have 1-5 more questions dropdown.
The way I'm doing this now is using jQuery .change()
and .hide()
/.show()
.
If the answer to a question without a sub question is Yes
, a dropdown with a date, textarea, and file input appears. If the answer to a question with a sub question or sub questions is Yes
, 1-5 more questions appear that when answered Yes
have a dropdown form with a date, textarea, and file input.
What's the best way to handle the markup, JS/jQuery, and ship the data to the backend?
As of now I'm doing something like this, and this ONLY accounts for the yes or no answer:
var answers = [];
for(var i = 1; i <= 42; i++) {
var question = $('#q' + i + '');
if(question.is(':checked'))
var answer = 'Yes';
else
var answer = 'No';
answers.push({
'question': answer
});
}
var content = {
'answers': answers
};
$.ajax({
url: '/questions',
type: 'POST',
data: content,
success: function(result) {
var result = JSON.stringify(result);
console.log(result);
}
});
I stop the loop at 42 because there are 42 questions. This gets complicated with the form and their respective class/id names and getting those values and sending the data.
What's the best way to do this to also send many date, text, and file input fields..?