jsp使用单个servlet在事件发生时动态地连续填充三个选择框

时间:2018-11-06 18:01:43

标签: javascript jsp servlets

1-我在jsp页面中有一个表单,该表单是在JavaScript的select选项上动态创建的。该表单具有三个空的选择框。

2-我有一个servlet来重定向jsp页面视图

3-我有一个数据库表,其中包含填充三个选择框的数据

问题:是否可以在一个选择事件中一个接一个地填充三个选择框,并且只用一个servlet来异步填充。

servlet代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
   Connection conn = MyUtils.getStoredConnection(request);
   String cursusc = request.getParameter("cursusc");
   /*PrintWriter pr =response.getWriter();
   response.setContentType("text/xml");*/

   if(cursusc=="look") {

   String errorString = null;

   List<Cursus> list = null;
   try {
       list = DButils.listetab(conn);
       System.out.println("code: ");
   } catch (SQLException e) {
       e.printStackTrace();
       errorString = e.getMessage();
   }

   request.setAttribute("errorString", errorString);
   request.setAttribute("etab", list);

   response.getWriter().append(" "+list);
   }

   RequestDispatcher dispatcher = 
   this.getServletContext().getRequestDispatcher("/WEB- 
   INF/views/client/cursus_info.jsp");
   dispatcher.forward(request, response);   

}

jsp首先选择代码:

<select name="etab" id="etab" onchange="lmdf()">
  <c:forEach items="${ETAB}" var="etablissement" >
    <option>${etablissement.etablissement}</option>
  </c:forEach>
  </select></td>

和第一个sql请求

公共静态列表listetab(Connection conn)抛出SQLException {

    String sql = "SELECT a.ETABLISSEMENT\r\n" + 
            "FROM CURSUS a";

    PreparedStatement pstm = conn.prepareStatement(sql);
    ResultSet rs = pstm.executeQuery();
    List<Cursus> list = new ArrayList<Cursus>();

    while (rs.next()) {
        String etab = rs.getString("ETABLISSEMENT");

        Cursus cursus = new Cursus();
        cursus.setEtablissement(etab);
        list.add(cursus);
    }
    return list;
}

js动态选择创建

 function cursuschoice(){
var selectBox = document.getElementById("choice");
var val= selectBox.value;

var origin1 = document.getElementById("td2");
var origin2 = document.getElementById("td3");
var origin3 = document.getElementById("td4");
var origin4 = document.getElementById("td5");

var origin11 = document.getElementById("rtd2");
var origin22 = document.getElementById("rtd3");
var origin33 = document.getElementById("rtd4");
var origin44 = document.getElementById("rtd5");

var myNode1 = document.getElementById("td2");
while (myNode1.firstChild) {
myNode1.removeChild(myNode1.firstChild);
}

var myNode2 = document.getElementById("td3");
while (myNode2.firstChild) {
myNode2.removeChild(myNode2.firstChild);
}

var myNode3 = document.getElementById("td4");
while (myNode3.firstChild) {
myNode3.removeChild(myNode3.firstChild);
}

var myNode4 = document.getElementById("td5");
while (myNode4.firstChild) {
myNode4.removeChild(myNode4.firstChild);
}

var myNode11 = document.getElementById("rtd2");
while (myNode11.firstChild) {
myNode11.removeChild(myNode11.firstChild);
}

var myNode22 = document.getElementById("rtd3");
while (myNode22.firstChild) {
myNode22.removeChild(myNode22.firstChild);
}

var myNode33 = document.getElementById("rtd4");
while (myNode33.firstChild) {
myNode33.removeChild(myNode33.firstChild);
}

var myNode44 = document.getElementById("rtd5");
while (myNode44.firstChild) {
myNode44.removeChild(myNode44.firstChild);
}

var s1 = document.createElement("span");
s1.setAttribute('id',"firsttd");
var s2 = document.createElement("span");
s2.setAttribute('id',"secondtd");
var s3 = document.createElement("span");
s3.setAttribute('id',"thirdtd");
var s4 = document.createElement("span");
s4.setAttribute('id',"fourthtd");

var s11 = document.createElement("select");
s11.setAttribute('name',"firstrtd");
var s33 = document.createElement("input");
s33.setAttribute('name',"thirdrtd");
var s44 = document.createElement("input");
s44.setAttribute('name',"fourthrtd");

if (val=="L"){
var t1= document.createTextNode("first");
var t2= document.createTextNode("second");
var t3= document.createTextNode("third");
var t4= document.createTextNode("fourth");

var s111 = document.createElement("select");
s111.setAttribute('name',"first1rtd");
origin11.appendChild(s111);

var s22 = document.createElement("input");
s22.setAttribute('name',"secondrtd");

}
else {
var t1= document.createTextNode("first +");
var t2= document.createTextNode("second +");
var t3= document.createTextNode("third +");
var t4= document.createTextNode("fourth +");

var s22 = document.createElement("select");
s22.setAttribute('name',"secondrtd");
}
s1.appendChild(t1);
s2.appendChild(t2);
s3.appendChild(t3);
s4.appendChild(t4);

origin1.appendChild(s1);
origin2.appendChild(s2);
origin3.appendChild(s3);
origin4.appendChild(s4);

origin11.appendChild(s11);
origin22.appendChild(s22);
origin33.appendChild(s33);
origin44.appendChild(s44);

}

0 个答案:

没有答案