gpt4 book ai didi

scala - Play Framework 复选框表单绑定(bind)

转载 作者:行者123 更新时间:2023-12-02 06:53:49 25 4
gpt4 key购买 nike

我在 html 模板中定义了以下复选框:

    <div class="answerDiv">
@if(exam.currentQuestion.get.quesType == "RADIO_BUTTON") {
<p>
@for(answer <- exam.currentQuestion.get.answers) {
<input type="radio" id="@answer.id" name="answerRadio" value="@answer.id">&nbsp;&nbsp;@answer.text</input><br>
}
</p>
}
@if(exam.currentQuestion.get.quesType == "CHECK_BOX") {
<p>
@for(answer <- exam.currentQuestion.get.answers) {
<input type="checkbox" name="answerCheckbox" value="@answer.id">&nbsp;&nbsp;@answer.text</input><br>
}
</p>
}
</div>

我想要 Controller 中定义了表单的复选框的值:

  val runExamForm = Form(
tuple(
"currentQuestionId" -> text,
"examId" -> text,
"totalQuestions" -> number,
"answerRadio" -> optional(number),
"answerCheckBox" -> optional(list(number))
)
)

在我的操作中,我正在获取表单值,但answerCheckBox 的值始终为 None。为什么是这样?我做错了什么?

最佳答案

好的。我让它工作了。这是我在 View 模板中所做的事情:

    <div class="answerDiv">
@if(exam.currentQuestion.get.quesType == "RADIO_BUTTON") {
<p>
@for(answer <- exam.currentQuestion.get.answers) {
<input type="radio" name="answerRadio" value="@answer.id">&nbsp;&nbsp;@answer.text</input><br>
}
</p>
}
@if(exam.currentQuestion.get.quesType == "CHECK_BOX") {
<p>
@for(answer <- exam.currentQuestion.get.answers) {
<input type="checkbox" id="@answer.id" name="answerCheckbox[@answer.id]" value="@answer.id">&nbsp;&nbsp;@answer.text</input><br>
}
</p>
}
</div>

注意复选框 html 渲染中的 name 属性。它必须是一个数组, Controller 中的绑定(bind)才能工作。

关于scala - Play Framework 复选框表单绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20822840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com