Set id of select options to jstl variable

放肆的年华 提交于 2019-12-11 09:52:58

问题


I've a JSP page that load some info from a DAO to a select element. Something like this:

<select id="dropdown" onchange="changeKa()">
  <c:forEach var="ka" items="${KaList}">
    <option value="<c:out value='${ka.area}' />" 
      <c:if test="${param.selectValue == ka.area})"> selected </c:if>  >
      <c:out value="${ka.area}" />
    </option>
  </c:forEach>
</select>

Where I have

<option value="<c:out value='${ka.area}' />" ... 

I want to set an id like this:

<option value="<c:out value='${ka.area}' />" id="<c:out value='${ka.id_knowledgearea}' />" ...

But the following error appears:

Bad value "   " for attribute "id" on element "option": An ID must not contain whitespace.

How can I solve this?


回答1:


Try to delete all useless spaces as follows:

<select id="dropdown" onchange="changeKa()">
    <c:forEach var="ka" items="${KaList}">
        <option value="<c:out value='${ka.area}'/>" <c:if test="${param.selectValue==ka.area})">selected</c:if>>
            <c:out value="${ka.area}"/>
        </option>
    </c:forEach>
</select>

<option value="<c:out value='${ka.area}'/>"

<option value="<c:out value='${ka.area}'/>" id="<c:out value='${ka.id_knowledgearea}'/>"



回答2:


Workaround...

So far I managed to overcome the problem by assigning the variable id_knowledgearea the label attribute of option. Like this:

<select id="dropdown" onchange="changeKa()">
     <c:forEach var="ka" items="${KaList}">
          <option value="<c:out value='${ka.area}'/>" label="<c:out value='${ka.id_knowledgearea}'/>"
              <c:if test="${param.selectValue == ka.area})"> selected </c:if>  >
              <c:out value="${ka.area}" />
          </option>
     </c:forEach>
</select>

Anyway, I'd like to understand how to assign the variable "id_knowledgearea" to the id of the element "option"...




回答3:


Try this:

<option value="<c:out value='${ka.area}' />" <c:if test="${param.selectValue == ka.area}">selected="selected" </c:if> id="<c:out value='${ka.id_knowledgearea}'/>">
    <c:out value="${ka.area}" />
</option>


来源:https://stackoverflow.com/questions/20845862/set-id-of-select-options-to-jstl-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!