【问题标题】:How to check radio button according to value retrieved from database?如何根据从数据库中检索的值检查单选按钮?
【发布时间】:2013-03-19 15:08:25
【问题描述】:

我有一个 JSP 页面,我在其中将所选单选按钮的表单和值保存在数据库中。现在,我需要更新该页面,显示所有内容,但未选择单选按钮。 我不知道如何在我的 jsp 上显示以前选择的单选按钮。我正在使用 Struts2,Java。

jsp代码:

    <div id="patientCondition">
            <input type="radio" id="new" value="n" name="pSB.radioInnerSubjective" /><label for="new">New</label>
            <input type="radio" id="noChange" value="nC" name="pSB.radioInnerSubjective" /><label for="noChange">No Change</label> 
            <input type="radio" id="progressing" value="p" name="pSB.radioInnerSubjective" /><label for="progressing">Progressing</label>
            <input type="radio" id="notProgressing" value="nP" name="pSB.radioInnerSubjective" /><label for="notProgressing">Not Progressing</label>
    </div>

假设我从数据库中获取单选按钮的值为“nC”,现在如何自动选择第二个单选按钮。

我试过了:

<input type="radio" id="new" value="n" <s:if test="${patientSoapBean.radioInnerSubjective == 'n'}">CHECKED</s:if> name="patientSoapBean.radioInnerSubjective"/><label for="new">New</label>

但我收到错误:

Mar 28, 2013 6:07:54 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: /WEB-INF/views/patient_soap.jsp (line: 703, column: 44) According to TLD or attribute directive in tag file, attribute test does not accept any expressions

【问题讨论】:

  • 我想我需要为此添加条件,但我不知道语法。
  • 代码显示不够完整,无法给出完整答案。当您遍历不同的可能答案时,您需要检查从数据库中检索到的保存值是否设置为当前答案的值。如果是,您需要将checked='checked' 添加到单选按钮的输入标签中。有很多方法可以做到这一点。
  • 你能指出我正确的语法吗?因为我很难弄清楚。我已经检查过,当我从数据库中取回值时,它反映了 pSB.radioInnerSubjective='nC',但是第二个选项(用于 'nC' 的选项没有得到检查。)。请帮帮我。
  • 你能展示更多你的代码吗?例如,你是如何得到答案的?
  • 我使用 ${} 通过 valuestack 从 action 类中获取值。例如&lt;input value="${pSB.patientID }" style="display: none" name="pSB.patientID"&gt;&lt;/input&gt;

标签: jsp struts2 radio-button el struts-tags


【解决方案1】:

您不能在 Struts2 标签中使用${...},您需要在test 属性中交换'" 才能正确比较一个字符串。

<input type="radio" id="new" value="n" <s:if test='patientSoapBean.radioInnerSubjective == "n"'>checked</s:if> name="patientSoapBean.radioInnerSubjective"/>
<label for="new">New</label>

当然,您还需要 patientSoapBeanradioInnerSubjective 的 getter/setter。

BTW Struts2 有&lt;s:radio&gt; 标签,它会检查选中的单选按钮。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-16
    • 2016-05-08
    • 2014-01-18
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多