javascript - 从选中的单选按钮传递整数数组而不是数字字符串

标签 javascript java spring type-conversion thymeleaf

焦点区域:看到 th:value="1"了吗?它通过 JavaScript 表单传递并作为字符串数组的一部分返回到 Controller 。是否可以将其转换为 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 来请求参数 ints/Integers 的数组/列表。我使用 SpringBoot 向 Controller 请求参数,如下所示:

 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[])

最佳答案

使用 Number() 将字符串转换为数字

checkedRadios.push(Number(radio.value))

关于javascript - 从选中的单选按钮传递整数数组而不是数字字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52510442/

相关文章:

javascript - 我如何停止流路由器中的订阅

java - 如何在 Spring 4.3 中实现 CSRF?

javascript - super 表达式必须为 null 或函数,而不是未定义的 - reactjs

javascript - 获取 jQuery 可排序列表中列表项的顺序

java - 使用Observable.interval等待远程数据或超时

java - 使用 Spring 正确测试 JPA DAO

java - spring添加redis-session-sharing功能后对象引用不同的内存地址

java - notnoop java-apns 抛出 javax.net.ssl.SSLHandshakeException :Received fatal alert: handshake_failure

JavaScript - 将一种方法应用于多个对象

java - 如何使用java从ftp服务器下载文件到本地计算机