2013年9月25日 星期三

DOM的寫法-

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<jsp:useBean id="mylist" scope="page" class="com.shaojung.shopping.CategoryDAO" />
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    pageContext.setAttribute("catlist", mylist.getlistbyMainID(0));
%>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script language="javascript" type="text/javascript">

var request = new XMLHttpRequest();

function funclick()
{   
    var id = document.getElementById("MainID").value;
    request.open("GET", "GetSubCategory.jsp?ID=" + id, false);
    request.onreadystatechange = updatePage;
    request.send(null);
}

function updatePage() {
    if (request.readyState == 4)
        if (request.status == 200)
            {
                 var ops = request.responseText.split('|');
                 var selectbox=document.getElementById("SubID");
  
               for (var j=ops.length-1;j>=0;j--)
                 {

                  
                  selectbox.remove(j);
                  }
                  for (var i=0;i<ops.length;i++)
                 {
                     var new_option = new Option(ops[i], i);
                     selectbox.options.add(new_option);
                 }     

               
            }
           
            // document.getElementById("show").innerHTML = request.responseText;
    }
</script>
</head>
<body>
  <p>
    <label for="MainID">分類</label>
    <select name="MainID" id="MainID" onchange="funclick()">
    <c:forEach items="${catlist}" var="item">
        <option value="${item.ID}">${item.CName}</option>
    </c:forEach>
    </select>
  </p>
  <p><span id="show"></span></p>
    <label for="SubID">子分類</label>
    <select name="SubID" id="SubID">

    </select> 
</body>
</html>

沒有留言:

張貼留言