Get value from drop down list in JSP/JSTL when option and c:out is used

醉酒当歌 提交于 2019-12-25 16:54:24

问题


I have the following JSP code for a select statement pulling a list of names stored in a database as nameIDs and names. The drop down shows the names (not the ids)

   <select  id="name" name="name" onchange="updateName(value)">
    <option/>
    <c:forEach items="${nameForm.nameList}" var="val">
    <option ${nameForm.name eq val.nameId?'selected':''} 
                         value="<c:out value="${val.nameId}"/>">
        <c:out value="${val.name}"/>
    </option>
    </c:forEach>
    </select>   

I'd like the updateName function to retrieve the value of the selected name. What the code below does is get the nameID not the value. I am not sure why value is returning an ID I am assuming val.nameId instead of the name selected in the list val.name

function updateName($1){

alert($1);

}

same thing if I use document.getElementById....


回答1:


And if you change this:

<select  id="name" name="name" onchange="updateName(value)">
<option/>
<c:forEach items="${nameForm.nameList}" var="val">
<option ${nameForm.name eq val.nameId?'selected':''} 
                     value="<c:out value="${val.nameId}"/>">
    <c:out value="${val.name}"/>
</option>
</c:forEach>
</select>   

Into this:

<select  id="name" name="name" onchange="updateName(value)">
<option/>
<c:forEach items="${nameForm.nameList}" var="val">
<option ${nameForm.name eq val.nameId?'selected':''} 
                     value="<c:out value="${val.name}"/>">
    <c:out value="${val.name}"/>
</option>
</c:forEach>
</select>   

Update

How about something like this: http://jsfiddle.net/robertrozas/y8e4B/



来源:https://stackoverflow.com/questions/24396695/get-value-from-drop-down-list-in-jsp-jstl-when-option-and-cout-is-used

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