1

I have a dropdown of roles in JSP file. When a role is selected from the dropdown I want to get the selected role ID. When a role is selected then an onchange event occured and a function 'callAjax(roleId) is triggered. Here the roleId is jsp variable. I am trying to pass it through a javascript. But I can not able to do this. Is it really possible? Can any one help me to doing this. Thanks in advance.

<select name="role" onchange="callAjax(roleId)">
 <c:forEach items="${pro.roles}" var="rol" varStatus="status">
   <option value="${rol.id}">
    <c:out value="${rol.name}"/>
  </option>
 </c:forEach>               
</select>

3 Answers 3

1

I guess you want to use the value which contains the id, so change to this:

 onchange="callAjax(this.value)"
Sign up to request clarification or add additional context in comments.

Comments

1

Remove the onchange="callAjax(roleId)" attribute from your select, and bind a change handler to the element with jQuery. The value returned by val will be the value from the selected option, which corresponds to the rol.id assigned to that option.

$(function() {
    $("select[name='role']").change(function() {
        callAjax($(this).val());
    });
});

Comments

0

try this : onchange="callAjax('<%=roleId%>')"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.