Get value from drop down list in JSP/JSTL when opt

2019-09-06 21:51发布

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条回答
地球回转人心会变
2楼-- · 2019-09-06 22:21

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/

查看更多
登录 后发表回答