81,094
社区成员
发帖
与我相关
我的任务
分享
very good.
<tr>
<td>
<b>节目类型</b>
</td>
<td>
<select name="bigTypeSelect" onchange="getSmallType(this.value);">
<option value="-1">
未选择
</option>
<c:forEach items="${bigTypeList}" var="temp">
<option value="${temp.typeId}">
${temp.typeName}
</option>
</c:forEach>
</select>
<select name="typeId">
<option value="-1">
未选择
</option>
</select>
</td>
</tr>
<script type="text/javascript">
//创建XMLHttpRequest对象
function createXMLHttpRequest()
{
//Mozilla 浏览器
if(window.XMLHttpRequest)
{
XMLHttpReq = new XMLHttpRequest();
}
// IE浏览器
else if (window.ActiveXObject)
{
try
{
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
}
//发送请求函数
function getSmallType(bigTypeId)
{
if(bigTypeId == -1)
{
var typeId = document.forms[0].typeId;
//清空select所有option选项
typeId.options.length = 0;
var option = document.createElement("option");
option.text = "未选择";
option.value = -1;
typeId.options.add(option);
return false;
}
createXMLHttpRequest();
XMLHttpReq.onreadystatechange = getFinsh;
var url = "getsmalltype.do?bigtypeid="+bigTypeId;
XMLHttpReq.open("post",url , true);
XMLHttpReq.send(null);
}
//处理返回响应函数
function getFinsh()
{
if (XMLHttpReq.readyState == 4)
{
if (XMLHttpReq.status == 200)
{
if(window.ActiveXObject)
{
var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
}
else
{
if(document.implementation && document.implementation.createDocument)
{
var xmlDom=document.implementation.createDocument("","doc",null);
}
}
xmlDom.async = false;
xmlDom.loadXML(XMLHttpReq.responseText);
var typeId = document.forms[0].typeId;
//清空select所有option选项
typeId.options.length = 0;
var smallTypes = xmlDom.getElementsByTagName("smalltypes/smalltype");
//包含子类信息
if(smallTypes.length != 0)
{
var option = document.createElement("option");
option.text = "未选择";
option.value = -1;
typeId.options.add(option);
for(var i = 0;i < smallTypes.length;i ++)
{
//子类ID
var typeIdText = smallTypes[i].firstChild.text;
//alert(typeIdText);
//子类名
var typeNameText = smallTypes[i].lastChild.text;
//alert(typeNameText);
//添加子类信息到下拉列表
var option = document.createElement("option");
option.text = typeNameText;
option.value = typeIdText;
typeId.options.add(option);
}
}
else
{
var option = document.createElement("option");
option.text = "无子类";
option.value = document.forms[0].bigTypeSelect.options(document.forms[0].bigTypeSelect.selectedIndex).value;
typeId.options.add(option);
}
}
else
{
alert("Error data:\n"+ XMLHttpReq.statusText);
}
}
}
</script>
//获得小类信息XML
public String getSmallTypeByBigTypeIdXml(String configFilePath,
String bigTypeId) {
File file = new File(configFilePath);
Configuration conf = new Configuration().configure(file);
SessionFactory sessionFactory = null;
sessionFactory = conf.buildSessionFactory();
Session session = sessionFactory.openSession();
Query query = session
.createQuery("from TypeInfo where parentId = :parentid");
query.setInteger("parentid", Integer.parseInt(bigTypeId));
List list = query.list();
session.close();
StringBuffer xmlSb = new StringBuffer("<smalltypes>");
for (int i = 0; i < list.size(); i++) {
TypeInfo typeInfo = (TypeInfo) list.get(i);
xmlSb.append("<smalltype><typeid>" + typeInfo.getTypeId()
+ "</typeid><typename>" + typeInfo.getTypeName()
+ "</typename></smalltype>");
}
xmlSb.append("</smalltypes>");
return xmlSb.toString();
}