当使用option和c:out时,从JSP/JSTL的下拉列表中获取值 [英] Get value from drop down list in JSP/JSTL when option and c:out is used
问题描述
对于以下select语句,我具有以下JSP代码:提取存储在数据库中的名称列表,作为nameID和名称.下拉列表显示名称(而不是ID)
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>
我希望updateName函数检索所选名称的值.下面的代码执行的操作是获取nameID而不是值.我不确定为什么值会返回一个我假定为val.nameId
而不是列表val.name
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){
function updateName($1){
alert($1);
}
如果我使用document.getElementById ...,也是如此.
same thing if I use document.getElementById....
推荐答案
如果您对此进行更改:
<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>
对此:
<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>
这篇关于当使用option和c:out时,从JSP/JSTL的下拉列表中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!