问题
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