I have one jsp and want to disable dropdown list using scriplet without any onclick method or event in dropdown menu
<%
boolean something= true;
if(something){
// here I want to get plc1 using id and want to disable it if it is true
}else{
//do some thing
}
%>
my dropdown html code is here
<td bgcolor='' align="center" ><select id = "plc1" name="place1" onclick="this.parentNode.style.backgroundColor = this.value">
<option value='red'>Park here</option>
<option value='green'>Cancel</option>
</select></td>
how to do it? any hint please?
just use disabled attribute: <select id="plc1" disabled>
<%
String state = "";
if(something){
state = "disabled";
}
%>
<select id="plc1" <%= state %>>
<%
boolean something=false;
String state = "";
if(something){
state = "disabled";
// here I want to get plc1 using id and want to disable it if it is ture
}else{
state = "enable";
}
%>
Html
<select <%= state %> id = "plc1" >
Now its working Thanks
Related
I have dropdown with options and the values. I can get the option value by the dropdown name in servlet but how can i get the dropdown "value" in servlet. In screenshot, temporarily i concatenated the with options but i want to store value in variable in servlet.
Please help:
HTML:
<input type="text" name="taxiDropdown" id= "taxiDropdown" placeholder="Search taxi...">
</div>
<div class="scrolling menu">
<%
List eList = (ArrayList) session.getAttribute("taxiInfo");
%>
<%
for (int i = 0; i < eList.size(); i++) {
%>
<div class="item" data-value="<%=((TaxiInfo) eList.get(i)).getID()%>">
<div class="ui green empty circular label"></div>
<%=((TaxiInfo) eList.get(i)).getTaxiPlate() +" "+ ((TaxiInfo) eList.get(i)).getID() %>
</div>
<%
}
%>
</div>
</div>
Servlet:
String val = request.getParameter("taxiDropdown");
(in "val", I want to store the value of the dropdown not the option text)
In JSP you should have something like that:
<form method="post">
<select name="taxiDropdown" id="taxiDropdown">
<%
List<TaxiInfo> eList = (List<TaxiInfo>) request.getAttribute("taxiInfo");
for (TaxiInfo taxiInfo : eList) {
%>
<option name="<%=taxiInfo.getTaxiPlate()%>" value="<%=taxiInfo.getID()%>"><%=taxiInfo.getTaxiPlate()%></option>
<%
}
%>
</select>
<input type="submit" />
</form>
Then in controller/servlet you will receive the id of TaxiInfo:
String val = request.getParameter("taxiDropdown");
System.out.println(val);
Or in your case you should set a hidden input with javascript with desired value.
added this code in html:
Move value of the dropdown selection to hidden textbox
<script type='text/javascript'>
$(function() {
$('#driverdp').change(function() { <-- this is my dropdown -->
var x = $(this).val();
$('#driverid').val(x); <-- this is my textbox -->
});
});
</script>
Servlet:
get the value of hidden text in servlet
String text= request.getParameter("driverId");
hope it will help someone
I am tying to alert the value of namesEmp variable and i get this Ljava.lang.String;#3433205b as result.
my javacsript:
function getNames(names) {
var namesEmp = "";
var namesEmpText = "";
for(i=0;i<names.length;i++) {
if(names.options[i].selected) {
namesEmp = names.options[i].value;
namesEmpText = names.options[i].text;
alert(namesEmp);
alert(namesEmpText);
}
}
}
and here is my HTML:
<tr style="position:relative;left:19;top:1;display:none;">
<td style="position:relative;top:1;text-align:right;">Country:</td>
<td>
<select id="namePay" name="namesPay" onChange="setTimeout('getNames(f.name)',1);">
<%
String namesDesc = "";
if(namesList.size()>0){
for(int i=0;i<namesList.size();i++){
String[] namesValues = ((String)namesList.get(i)).split("~");
NamesText = namesValues[0];
%>
<option value="<%=namesValues%>"><%=NamesText%>
<%}
}%>
</select>
</td>
</tr>
When I alert namesEmpText, i get the the correct result. Any help would be appreciated.
The issue is here:
<option value="<%=namesValues%>"><%=NamesText%>
^
You are trying to render the Java object namesValues (a string array), as a select option value. The JVM doesn't know how to automatically convert an array to a string, so it renders the type name for it instead. Did you mean to include an indexer to that array? For example:
<option value="<%=namesValues[1]%>"><%=namesValues[0]%>
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 " ".
Can someone please tell me how to set a checkbox dynamically in Javascript?
function displayResults(html){
var v = html.split(",");
document.getElementById('fsystemName').value = v[0];
if (v[1] == "true"){
document.getElementById('fUpdateActiveFlag').checked = true;
}
}
So I am passing some values from my controller separated by commas. For the checkbox, when the value is true I want it to tick the box.
EDIT:
When a value is changed from a dropdown box it calls this displayResults method as its return statement:
$('#uINewsSystemList').change(function() {
$.get("/live-application/SystemById", {systemid: $("#systemList").val()}, displayResults, "html");
I want it to update some other values such as textboxes and checkboxes. I can get the fsystemName to populate with the appropriate value but the fUpdateActiveFlag always stays unchecked.
In your question, you posted a javascript example, not JSP.
In JSP you can use a for loop to write the checkbox like this:
<% for (Element element : elementList) { %>
<input type="checkbox" name="<%=element.getName() %>" value="<%=element.getValue() %>" <%=element.getChecked() ? "checked" : "" %> />
<% } %>
Will result in:
<input type="checkbox" name="option1" value="Milk"> Milk<br>
<input type="checkbox" name="option2" value="Butter" checked> Butter<br>
<input type="checkbox" name="option3" value="Cheese"> Cheese<br>
I have a database driven textbox that needs to be repopulated when the user hits the back button (a back button that I have created on the form)
Currently, I am able to repopulate non database driven fields on the form using http sessions just fine. I cannot seem to apply to same logic to database driven fields.
The code on my jsp looks as follows:
<td><select name = "actionType" tabindex = "1" value="<%if(session.getAttribute("actionType")== null) out.print(""); else out.print(session.getAttribute("actionType"));%>">
<option>--</option>
<% for(int i=0; i<actTypeDDL.size()-1; i++){
String actType = actTypeDDL.get(i).toString();
i++;
String actTypeVal = actTypeDDL.get(i).toString();%>
<option value=<%=actTypeVal%>>
<%=actType%>
</option>
<%
} %>
</select></td>
Any ideas?
The issue doesn't have anything to do with the fact that the value comes from the database, the problem is that specifying a value on a select tag won't preselect the option. You need to add a "selected" flag to the option itself.
The following should work:
<td><select name = "actionType" tabindex = "1" >
<option>--</option>
<% for(int i=0; i<actTypeDDL.size()-1; i++){
String actType = actTypeDDL.get(i).toString();
i++;
String actTypeVal = actTypeDDL.get(i).toString();%>
<option value=<%=actTypeVal%>
<% if (session.getAttribute("actionType") == actTypeVal) {
System.out.println("selected = 'true'");
} %>
>
<%=actType%>
</option>
<%
} %>
</select></td>