I have below code in my jsp :
<select name="productId" id ="productId" onchange="getDetail()">
<% for (int i = 0;i < no_rows;i++){
%>
<option value="<%=ar[i]%>"><%=ar[i]%></option>
<%
}
%>
</select>
On page refresh(F5), the values in the drop down is not getting refreshed.The previously selected value remain there. DB call to fetch the detail is in JSP itself.
How to refresh the values?
Try JSTL <c:forEach> Tag instead
<select name="productId" id ="productId" onchange="getDetail()">
<c:forEach var="item" items=${yourListNameStoredInSomeScope}>
<option value="{item}">${item}<option>
</c:forEach>
</select>
try something like this:
<select name="productId" id ="productId" onchange="getDetail()">
<option value=" " selected></option>
<% for (int i = 0;i < no_rows;i++){
%>
<option value="<%=ar[i]%>"><%=ar[i]%></option>
<%
}
%>
</select>
if you use selected attribute , when the drop down is loaded, the pre-selected option is the default in your case " ".
Related
so i'm trying to pass selected value from a combobox in a jsp to another jsp here is what i got myself into
<form name="ff" method="post">
<select name='mat' id='soflow'>
<% ArrayList<Matiere> listeM = MatiereListe.GetMatiere(); %>
<option></option>
<% for (Matiere d : listeM)
{ %>
<option value= <%=d.getCode_mat() %> >
<%=d.getLib_mat() %>
</option>
<% } %>
</select>
<input type=submit value=valider />
<br>
</form>
<button onclick="window.location.href='/acceuil.jsp?mat=' JSGetSelectedItemMat() " >Statistique</button>
<script>
function JSGetSelectedItemMat()
{
var e = document.getElementById("soflow");
var strSel = e.options[e.selectedIndex].value;
document.getElementById("btt").value = strSel;
}
</script>
try this code to get selected valued from select tag. As there is no need to use js, you are using post method in your html form. Also get the value in acceuil.jsp using getParameter method. that is request.getParameter("mat");
<form action="acceuil.jsp" name="ff" method="post">
<select name='mat' id='soflow'>
<% ArrayList<Matiere> listeM = MatiereListe.GetMatiere(); %>
<option></option>
<% for (Matiere d : listeM)
{ %>
<option value= <%=d.getCode_mat() %> >
<%=d.getLib_mat() %>
</option>
<% } %>
</select>
<input type=submit value=valider />
<br>
</form>
I want to display selected value. In the text field I can display it within value like below
value ="<%=event_data.getE_venue()%>"
code :
<input type="text" name="where" placeholder="Add a place" size="23" value ="<%=event_data.getE_venue()%>"/>
<select name="category" value ="<%=event_data.getE_target_category()%>" id="single1">
<option>Sports</option>
<option>Corporate</option>
<option>Religious</option>
<option>Music</option>
</select>
but in dropdown box it doesn't work.
please help me. thanks..
Firstly, select doesn't work in that way , you need to put selected attribute in option that matches your input.
for example:
<option selected='selected'>Sports</option>
check this fiddle :
http://jsfiddle.net/ZLTS7/
your code should be something like :
<input type="text" name="where" placeholder="Add a place" size="23" value ="<%=event_data.getE_venue()%>"/>
<select name="category" id="single1">
<option <%= (event_data.getE_target_category().equals("Sports")?"selected='selected'":"") %>>Sports</option>
<option <%= (event_data.getE_target_category().equals("Corporate")?"selected='selected'":"") %>>Corporate</option>
<option <%= (event_data.getE_target_category().equals("Religious")?"selected='selected'","") %>>Religious</option>
<option <%= (event_data.getE_target_category().equals("Music")?"selected='selected'":"") %>>Music</option>
</select>
You need to change the dropdown value through Javascript or jQuery. You can assign desired value to dropdown just like input
var dd = document.getElementById('single1');
var opts = ddl.options.length;
var value = <%=event_data.getE_venue()%>;
for (var i=0; i<opts; i++){
if (dd.options[i].value == value){
dd.options[i].selected = true;
break;
}
}
or if you are using jQuery.
$("#single1").val(value);
See this example:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#slectboxid option').click(function(){
$('#textboxid').val($(this).val());
});
});
</script>
</head>
<body>
<form action="#" method="post">
<select name="select" id="slectboxid">
<option value="test">test</option>
<option value="test2">test2</option>
</select>
<input type="text" name="text" id="textboxid" />
</form>
</body>
</html>
I'm tying to bind the value to dropdownlist i.e named as subtype which is depending on another dropdownlist i.e named as type. Please tell me how I can do that.. I have given I following code which implement for binding the dropdownlist depending on another dropdownlist.
`<table>
<tr>
<td>
<label id="type">Chosen category : </label>
</td>
<td>
<% String getType=request.getParameter("id");
out.println("<h4>"+getType+" <a href='post free ads.jsp'>Change the
category</a> </h4>");
%>
</td>
</tr>
<tr>
<td>
<label id="typeselect">Select type : </label>
</td>
<td>
<select name="type">
<option value="none">Select</option>
<%
Connection conn = NewClass.getOracleConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select distinct company from admin.all_stuff
where stuff_type='"+getType+"' ");
while(rs.next()){
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(1)%></option>
<%
}
%>
</select>
</td>
</tr>
<tr>
<td> <label id="subtypeselect">Select Subtype : </label></td>
<td>
<select name="subtype">
<option value="none">Select</option>
<%
try
{ %>
<% String typeselected = request.getParameter("type");
Statement stmtmt = conn.createStatement();
ResultSet rse = stmtmt.executeQuery("Select model_no from admin.all_stuff
where stuff_type='"+getType+"' and company='"+typeselected+"' ");
while(rse.next()){
%>
<option value="<%=rse.getString(1)%>"><%=rse.getString(1)%></option>
<%
}
%>
<% }
catch(Exception e)
{
out.println(e.getMessage());
}
%>
}
</select>
</td>
</tr>
</table>`
You need AJAX for this to get it work.
Your jsp is compiled into a servlet and then delivered to your client. After that no more changes are possible.
You have to handle an onChange event on the first drop down box in HTML and then post an AJAX request to your server to send back the content for the second drop down box.
Easiest way to start with AJAX is to get some library for example jQuery
$.get('ajax/test.html', function(data) {
alert(data);
});
This code allow us to download from HTML page some content from URL 'ajax/test.html'.
Second argument is callback function. Variable data has content of page 'ajax/test.html'
More: http://api.jquery.com/jQuery.get/
I have the controller file which holds a list of values in the model like,
List caseIds = new ArrayList();
for(int i=0; i<caseLists.size(); i++)
{
if(caseIds.contains(caseLists.get(i).getCaseId()))
continue;
else
caseIds.add(caseLists.get(i).getCaseId());
}
model.put("caseIdList", caseIds);
and i want to show this caseIds in UI in a dropdown box. My code looks like this,
<select name="caseIds" id="caseIds">
<option value="">All</option>
<c:forEach var="item" items="${model.caseIdList}">
<option value="<c:out value='${item.caseIdList}'/>" >
<c:out value="${item.caseIdList}"/>
</option>
</c:forEach>
</select>
I mean I want the dropdown(select box) with an id and value as caseId's. I understand that there is an error in getting item with item.caseIdList. How should I approach?
item in the for loop in the jsp will be the caseId that was added with caseLists.get(i).getCaseId(), so I think you just want the following:
<select name="caseIds" id="caseIds">
<option value="">All</option>
<c:forEach var="item" items="${model.caseIdList}">
<option value="<c:out value='${item}'/>" >
<c:out value="${item}"/>
</option>
</c:forEach>
</select>
This is what you want to do :
<select name="caseIds" id="caseIds">
<option value="">All</option>
<c:forEach var="item" items="${model.caseIdList}">
<option value="<c:out value='${item}'/>" >
<c:out value="${item}"/>
</option>
</c:forEach>
</select>
You need to use item instead of item.caseIdList since the caseIdList contains Integer.
I have the below code that display list of values from database in drop down. The query also selects Jt_JOB_Description. I would like to display the Jt_JOB_Description based on the JT_JOB_TITLE selected in Text Area. Help plz.
<select name="jTitle" id="jTitle" style="background-color: #D8D8D8">
<%
Statement stt = conn.createStatement();
ResultSet rstt = stt.executeQuery("SELECT JT_JOB_TITLE, JT_JOB_DESCRIPTION FROM uap.dbo.UAP_JOB_TITLE ORDER BY JT_JOB_TITLE");
while (rstt.next()) {
%>
<option value="<%=rstt.getString("JT_JOB_TITLE")%>"> <%=rstt.getString("JT_JOB_TITLE")%>
</option>
<%
}
stt.close();
rstt.close();
%>
</select>
<textarea class="styled" rows="5" cols="12" name="jDesc" id="jDesc" ></textarea>
Modify your code as given below
<select name="jTitle" id="jTitle" style="background-color: #D8D8D8" onchange="setText(this)">
<%
String str="";
Statement stt = conn.createStatement();
ResultSet rstt = stt.executeQuery("SELECT JT_JOB_TITLE, JT_JOB_DESCRIPTION FROM uap.dbo.UAP_JOB_TITLE ORDER BY JT_JOB_TITLE");
while (rstt.next()) {
str+=rstt.getString(1)+"#"+rstt.getString(2)+"#";
%>
<option value="<%=rstt.getString("JT_JOB_TITLE")%>"><%=rstt.getString("JT_JOB_TITLE")%>
</option>
<%
}
stt.close();
rstt.close();
%>
</select>
<input type="hidden" name="txtHidStr" id="txtHidStr" value="<%=str %>" />
<textarea class="styled" rows="5" cols="12" name="jDesc" id="jDesc" ></textarea>
and wirte following javascript function
function setText(element){
var arr_main=document.getElementById("txtHidStr").value.split("#");
for(i=0;i<arr_main.length;i++)
{
arr_val=arr_main[i].split("#");
if(arr_val[0]==element.value)
{
document.getElementById("jDesc").innerHTML=arr_val[1];
break;
}
}
}