【问题标题】:How to retain select box value如何保留选择框值
【发布时间】:2014-09-17 07:07:06
【问题描述】:

我有一个选择框。有 4 个值(使用 jstl 标签生成)和加载按钮。

我在点击加载按钮时调用 struts 动作

在 struts 动作类中我添加了request.getAttribute("TA",value);

现在我想在jsp页面中保留之前选择的值

                        <%if(request.getAttribute("TA")!=null && request.getAttribute("TA").equals("Cloud")) {%>
                            <option value='${i.thematicArea}' selected>${i.thematicArea}</option>
                        <%} else {%>     
                            <option value='${i.thematicArea}'>${i.thematicArea}</option>
                            <%} %>
                        </c:forEach>
                        </select>
                        </td>                   
                        <td><input type="submit" id="button" name="button" value="Load"></td>

我想与${i.thematicArea} 比较而不是云。如何使用 jstl 做到这一点 例如:

request.getAttribute("TA").equals("Cloud")) 
instead of cloud i want to check with ${i.thematicArea}

提前致谢

【问题讨论】:

    标签: jsp jakarta-ee struts2 jstl jsp-tags


    【解决方案1】:

    类似的东西

    <c:if test="${not empty TA and TA eq i.thematicArea}">
      <option value='${i.thematicArea}' selected>${i.thematicArea} </option>
    </c:if>
    <c:if test="${empty TA or not TA eq i.thematicArea}">
      <option value='${i.thematicArea}'>${i.thematicArea}</option>
    </c:if>
    

    如果您想在option 标签上应用不同的标记,请使用此选项。

    【讨论】:

      【解决方案2】:

      一种干净的方式是这样的(使用三元运算符将其减少为一行):

      <select>
      
      <c:forEach items="..." var="i">
      
         <option value='${i.thematicArea}' ${requestScope.TA == i.thematicArea ? 'selected' : ''}>${i.thematicArea}</option>
      
      </c:forEach>
      
      </select>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多