Область фокуса: см. Th: value = "1"?Это передается через форму JavaScript и возвращается контроллеру как часть массива строк.Можно ли преобразовать это в int?
<input
type="radio"
th:name="'userAnswer' + ${iter.index}"
th:value="1"
th:attr = "data-question-id=${question.value}, data-question-key=${question.key}" />
Остальное здесь, если вам нужно больше контекста.
model.addAttribute("title", "Take The Test!");
model.addAttribute("currentMatchingTestQuestions", questions);
model.addAttribute("newList", shuffleMap);
model.addAttribute("test",currentTest);
<div th:each="question, iter : ${newList}" name="questions" class="list-group">
<h2 th:text="${question.key}" class="list-group-item-heading">User Display Name</h2>
<div class="form-group">
<br></br>
<label th:for="desiredAnswer1">Desired Answer</label>
<br></br>
<!--maybe put div for each hidden input?-->
<!--iter. index is so that each name is different, so we can click one button per question.-->
<input
type="radio"
th:name="'userAnswer' + ${iter.index}"
th:value="1"
th:attr = "data-question-id=${question.value}, data-question-key=${question.key}"
/> Always True
<!--<input type="hidden" th:value="${question.id}" name="questionId" />-->
<input type="hidden" th:name="userAnswer" th:value="1" class="userAnswers"/>
<br></br>
<input
type="radio"
th:name="'userAnswer' + ${iter.index}"
th:value="2"
th:attr = "data-question-id=${question.value}, data-question-key=${question.key}"
/> Mostly True
<!--<input type="hidden" th:value="${question.id}" name="questionId" />-->
<input type="hidden" th:name="userAnswer" th:value="2" class="userAnswers" />
<br></br>
<input
type="radio"
th:name="'userAnswer' + ${iter.index}"
th:value="3"
th:attr = "data-question-id=${question.value}, data-question-key=${question.key}"
/> Sometimes True Sometimes False
<!--<input type="hidden" th:value="${question.id}" name="questionId" />-->
<input type="hidden" th:name="userAnswer" th:value="3" class="userAnswers"/>
<br></br>
<input
type="radio"
th:name="'userAnswer' + ${iter.index}"
th:value="4"
th:attr = "data-question-id=${question.value}, data-question-key=${question.key}"
/> Mostly False
<!--<input type="hidden" th:value="${question.id}" name="questionId" />-->
<input type="hidden" th:name="userAnswer" th:value="4" class="userAnswers"/>
<br></br>
<input
type="radio"
th:name="'userAnswer' + ${iter.index}"
th:value="5"
th:attr = "data-question-id=${question.value}, data-question-key=${question.key}"
/> Always False
<!--<input type="hidden" th:value="${question.id}" name="questionId" />-->
<input type="hidden" th:name="userAnswer" th:value="5" class="userAnswers"/>
<br></br>
</div>
</div>
function findChecked() {
const checkedRadios = [];
const questionIds = []; //arrays
const questionKeys = [];
const answerInput = document.querySelector('#allAnswers'); //why is this declared twice again?
const questionIdInput = document.querySelector('#questionIds');//sets variable for hidden input of questionIds
const questionKeysInput = document.querySelector('#questionKeys');
const radios = document.querySelectorAll('input[type=radio]');// sets variable for all radio button values
radios.forEach(radio =>{ //loops through radio values
if(radio.checked){ // if the radio button is checked
checkedRadios.push(radio.value); //push the value of radio button to checkedRadios array
questionIds.push(radio.dataset.questionId);//push question.Id to questionIds array. Notice that dataset interprets...
questionKeys.push(radio.dataset.questionKey); //...question.id as questionId
}
});
answerInput.value = checkedRadios; // pass array of checked radios to answerInput for the id of "allAnswers" hidden input
questionIdInput.value = questionIds;// same as above but for id="questionIds" hidden input
questionKeysInput.value = questionKeys;
console.log(checkedRadios);//debug
console.log(questionIds);
}
Я использую форму JavaScript, чтобы получить каждый пользовательский ответ и поместить его в массив.Однако прямо сейчас он может отправлять только массив строк чисел.Есть ли способ использовать JS или Thymeleaf для запроса параметров массив / список целых / целых чисел.Я использую SpringBoot для запроса параметров в контроллер как:
public String processTakeTest(Model model, @PathVariable int testId, HttpSession session, @RequestParam(name="allAnswers") String allAnswers[],
@RequestParam(name="questionIds") String questionIds[], @RequestParam(name="questionKeys") String questionKeys[])