【问题标题】:same option gets selected two times in drop down , how this can be stopped?相同的选项在下拉列表中被选中两次,如何停止?
【发布时间】:2015-06-10 16:00:19
【问题描述】:

我正在尝试构建一个用户提交票证并可以在提交后稍后对其进行编辑的项目。在创建工单页面上有一个下拉菜单,用于选择工单的优先级选项 - 最高、高、中、低。数据库中有一个优先选项表。当用户单击与工单对应的编辑链接时,用户将被转发到一个编辑页面,其中显示了工单的不同字段。为了显示优先菜单的选定选项,我正在这样做,如下

<select name="priority">
 <c:forEach var="priority"items="${priorityList }">
    <c:if test="${tempTicket.priorityId == priority.priorityId}">
        <option value="${priority.priorityId }" selected="selected">${priority.priorityName}</option>
    </c:if>
   <option value="${priority.priorityId }">${priority.priorityName </option>
 </c:forEach>
</select>

在此选择的选项在编辑页面的下拉列表中显示两次,如何停止?有没有其他方法可以实现相同的目的?我尝试使用两个 &lt;c:if&gt; 。

【问题讨论】:

标签: java html jsp servlets jstl


【解决方案1】:

第一个解决方案:

<c:if test="${tempTicket.priorityId != priority.priorityId}">
    <option value="${priority.priorityId }">${priority.priorityName }</option>
</c:if>

第二种解决方案:

<c:choose>
    <c:when test="${tempTicket.priorityId != priority.priorityId}>
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>

第三种(也是最好的)解决方案:

<option value="${priority.priorityId }" 
        <c:if test="${tempTicket.priorityId == priority.priorityId}">selected="selected"</c:if>>
    ${priority.priorityName}
</option>

【讨论】:

    【解决方案2】:

    jstl中的其他实现是

    选择标签

    <c:choose>
      <c:when test="cond1">
        Condition 1
      </c:when>
      <c:otherwise>
        Condition 2 (if condition 1 is false)
      </c:otherwise>
    </c:choose>
    

    【讨论】:

      【解决方案3】:

      试试这个

      <option value="${priority.priorityId }" 
      ${tempTicket.priorityId == priority.priorityId? "selected" : ""} 
      >${priority.priorityName}</option>
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      相关资源
      最近更新 更多