user20432
user20432

Reputation: 107

Selected value for JSP drop down

In my JSP page i'm using a popup to add new precipitants to the dropdown list. its working fine but problem is if i add one, the dorpdown shows "Add New" instead of "--Please Select--"

This is what i tried later

<c:if test="${listop.precipitants eq listop.precipant}">Selected </c:if>

and getting Unterminated <form:option tag exception

here is my controller code

    map.put("precipitantdroplist", bioprofileservice.listPrecipitantdrop());
    map.put("Precipitantdropfam", new Precipitantsdrop());

and in JSP

<form:select  path="precipant" id="precipant" onchange="showpopup(this.options[this.selectedIndex].value,this.form,'Newprecipant?#login_form3');">
 <form:option value="">--Please Select--</form:option>
 <c:if test="${!empty precipitantdroplist}">
                <c:forEach items="${precipitantdroplist}" var="listop">
                <form:option value="${listop.precipitants}" <c:if test="${listop.precipitants eq listop.precipant}">Selected </c:if>>${listop.precipitants}</form:option>       
                </c:forEach>
          </c:if>
 <form:option value="">-----------------------</form:option>
                <form:option value="Add New">Add New</form:option>
                <form:option value="removeP">Remove</form:option>
  </form:select>

<-------- edited here---------->

this is how my drop down look like

enter image description here

once i click "Add New" its look like this, but i want "--Please Select--" instead "Add New" (see the image below) enter image description here

after i add "meme" my dropdown look like this

enter image description here

but i want like this enter image description here

Upvotes: 2

Views: 4869

Answers (1)

Ankur Singhal
Ankur Singhal

Reputation: 26067

Try this, modify if condition according to yours if required.

    <form:select  name="precipant" path="precipant" id="precipant" onchange="showpopup(this.options[this.selectedIndex].value,this.form,'Newprecipant?#login_form3');">
 <form:option value="" selected="selected">--Please Select--</form:option> 

    <c:forEach items="${precipitantdroplist}" var="listop">
            <c:if test="${listop.precipitants eq listop.precipant}">
                <option value="${listop.precipitants}">${listop.precipitants}</option>
            </c:if>
            <c:if test="${listop.precipitants != listop.precipant}">
                <option value="${listop.precipitants}">${listop.precipitants}</option>
            </c:if>
    </c:forEach>

    <form:option value="">-----------------------</form:option>
    <form:option value="Add New">Add New</form:option>
    <form:option value="removeP">Remove</form:option>
</form:select>

or register the handler for the dropdown like this,

<script type="text/javascript">

    $(document).ready(function() {

        $("#precipant").change(function() {
            $('select[name^="precipant"] option:selected').attr("selected",null);
            $('select[name^="precipant"] option[value="Please Select"]').attr("selected","selected");
        });

    });

    </script>

Upvotes: 2

Related Questions