Set id of select options to jstl variable

2019-07-30 06:45发布

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?

3条回答
倾城 Initia
2楼-- · 2019-07-30 06:55

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楼-- · 2019-07-30 07:02

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>
查看更多
聊天终结者
4楼-- · 2019-07-30 07:04

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}'/>"
查看更多
登录 后发表回答