I'm trying to make generic version of a form where dropdown would appear depending on previous dropdown value, but some dropdown is dependent on two or more previous answers.
The problem I encountered is that got questions which are dependent on the same question with same answer, so when I iterate JSON, it shows them both, meanwhile second question is suppose to appear only when all dependent answers are fulfilled, so I need a way to separate them. Currently, questions with Id 8 and 9 have same dependent answers, but question 9 has one more dependency.
JSON looks like this:
var questions = [
//questions before these
{
Id: 6,
ProductGroups: [{
ProductGroupId: 1,
show: false
},
{
ProductGroupId: 2,
show: true
}
],
//dependant answer(s)
DependantAnswers: [{
QuestionId: 1,
answer: ""
}]
},
{
Id: 7, //guid
ProductGroups: [{
ProductGroupId: 1,
show: false
},
{
ProductGroupId: 2,
show: false
}
],
//dependant answer(s)
DependantAnswers: [{
QuestionId: 6,
answer: "male"
}]
},
{
Id: 8, //guid
ProductGroups: [{
ProductGroupId: 1,
show: false
},
{
ProductGroupId: 2,
show: false
}
],
//dependant answer(s)
DependantAnswers: [{
QuestionId: 6,
answer: "female"
}
]
},
{
Id: 9, //guid
ProductGroups: [{
ProductGroupId: 1,
show: false
},
{
ProductGroupId: 2,
show: false
}
],
//dependant answer(s)
DependantAnswers: [{
QuestionId: 6,
answer: "female"
},
{
QuestionId: 8,
answer: "yes"
}
]
}
];
And this is jQuery function:
function onQuestionSelectChange() {
$("#questionsContainer div select").change(function () {
var selectedValue = $(this).val();
var selectedQuestion = $(this).parent().attr('id').split('-');
var selectedQuestionId = selectedQuestion[1];
var potentialQuestions = [];
//var filteredQuestions = [];
$.each(questions, function(index, element) {
$.each(element.DependantAnswers, function (indexDepAnswer, elemDepAnswer){
if(elemDepAnswer.answer == selectedValue)
potentialQuestions.push(element);
});
});
//here I need to separate question 8 from 9 and show only question 8
});
}
With above code I get an array of 2 objects filled with question 8 and 9, but question 9 needs to appear only when question 8 is answered with value "yes". Whatever "if" I tried, question 9 passes just like question 8, because it has same dependent answers. How can I filter question 9 and show it only after I picked "yes" on question 8?