How to: set the selected "drop down list" value at run time
The pasted below code i am using to create dropdown list and java script for doing some stuffs
<td>
<select id="currentCustomer" onchange="showSelectedCustomer(this)">
<c:forEach var="Customer" items="${listCustomer}" >
<option value="<c:out value="${Customer}" />"><c:out value="${Customer}" />
</option>
</c:forEach>
</select>
</td>
function showSelectedCustomer(dropdown) {
var selectedCustomer = dropdown.options[dropdown.selectedIndex].value;
var currentCustomer = document.getElementById('currentCustomer');
var context = document.forms[0].context.value;
document.forms[0].mode.value = "UPDATE";
document.forms[0].custName.value = dropdown.options[dropdown.selectedIndex].value;
document.forms[0].action=context+"/updateReportDetail.do";
document.forms[0].method="POST";
document.forms[0].submit();
}
My problem is how to set the onselected value dynamically in drop down list.
Let me explain briefly here,
Initially at the time of logging application the landing "x.JSP" page will display drop down list with multiple customer names with first customer name as selected one by default. If i select other customer name in drop down list i am calling Javascript[onchage event] to submitting to form and to do开发者_运维百科 some other stuffs in DAO java class for the selected customer and returning back to "x.JSP" page.
Now the thing is i need to set the latest customer name [ie..the customer i processed in DAO java class] as onselected one in the drop down list.
Help me guys.Thanks for your effort in advance.
In your forEach loop, check if the Customer in your current iteration is the one that matches your predetermined choice. You don't give any indication of how you're choosing the "right" customer to be selected when the page loads, so I'll use some pseudo-code for that.
<c:forEach var="Customer" items="${listCustomer}" >
<% String selected = "";
if (customer.equals(predeterminedCustomerToBeSelected)) {
selected = "selected"; }
%>
<option value="<c:out value="${Customer}" />" <%=selected%>><c:out value="${Customer}" />
</option>
</c:forEach>
精彩评论