javascript - Dynamically change JSP page content without buttons -
i've written code dynamically fills drop down list sql database. next, need change content of page depending on user selection in real-time (no buttons), perhaps show label, or show drop down list gets it's values sql db depending on previous selection etc.. here code
<%@ page import="java.sql.*" %> <%resultset resultset = null; connection conn;%> <html> <% try { class.forname("com.microsoft.sqlserver.jdbc.sqlserverdriver").newinstance(); conn = drivermanager.getconnection("jdbc:sqlserver://desktop-o708f3g\\sqlexpress;databasename=master", "user", "pswd"); statement statement = conn.createstatement(); resultset = statement.executequery("select * rtable"); %> <h1> drop down list</h1> <select name="projectname"> <% while (resultset.next()) {%> <option><%= resultset.getstring(2)%></option> <% } %> </select> <% } catch (exception e) { out.println("error"+e); } %> </html>
after selections done, display "submit" button sends selected data back-end servlet (i know how once button there).
you can write following javascript function , call on onchange event of <select>
element:
<p id="demo"></p> <script> function myfunction() { var x = document.getelementbyid("myselect").value; document.getelementbyid("demo").innerhtml = "you selected: " + x; } </script> //your jsp code <select name="projectname" id="myselect" onchange="myfunction()"> <% while (resultset.next()) {%> <option value="<%= resultset.getstring(2)%>"><%= resultset.getstring(2)%></option> <% } %> </select>
please check code value attribute of <option>
, might wrong; other code work fine. have added 'id' , 'onchange' attributes <select>
tag.
Comments
Post a Comment