(This project is being developed with Spring Boot + Thymeleaf) I have an html page that will work as a questionnaire containing more or less 100 questions (the code below is just an example). Each question has 4 inputs.
<div>
<input class="question-option" type="radio" th:name="${question.id}" th:data-correct="${question.correctOption}" value="a" /><span th:text="${question.optionA}"></span>
<input class="question-option" type="radio" th:name="${question.id}" th:data-correct="${question.correctOption}" value="b" /><span th:text="${question.optionB}"></span>
<input class="question-option" type="radio" th:name="${question.id}" th:data-correct="${question.correctOption}" value="c" /><span th:text="${question.optionC}"></span>
<input class="question-option" type="radio" th:name="${question.id}" th:data-correct="${question.correctOption}" value="d" /><span th:text="${question.optionD}"></span>
</div>
I want to get, via JavaScript (JQuery), all inputs that are "checked" and store them in a JSON and then send that object to a controller via Ajax.
function getAllResults() {
var inputs = $('input:radio:checked');
for (var i = 0; i < inputs.length; i++) {
//inserir os dados em um objeto JSON
}
My question is (if at all possible), how to store the data within JSON? Example:
("answer" would be the "value" attribute and "correctOption" would be the "th: data-correct" attribute)
[
{"answer": c, "correctOption": b}
{"answer": a, "correctOption": d}
{"answer": d, "correctOption": a}
{"answer": b, "correctOption": c}
]