//创建XMLHttpReques对象
function createXMLHttpRequest() {
if (window.XMLHttpRequest) {
//Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
} else{
// IE浏览器
if (window.ActiveXObject) {
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e) { }
}
}
}
}
//处理服务器响应结果
function handleResponse() {
// 判断对象状态
if (XMLHttpReq.readyState == 4) {
// 信息已经成功返回,开始处理信息
if (XMLHttpReq.status == 200) {
var out = "";
var res = XMLHttpReq.responseXML;
clearList();
var subClass = res.getElementsByTagName("toyop");
for(var i=0;i<subClass.length;i++)
{
var top = subClass(i).getElementsByTagName("top")[0].firstChild.nodeValue;
var sub = subClass(i).getElementsByTagName("sub")[0].firstChild.nodeValue;
addOption(top,sub);
}
}
}
}
//刷新第二个下拉列表
function refreshList()
{
var toyClass = document.getElementById("toyClass").value;
var toyUrl="/toysshop/servlet/DynamicList?toyClass="+toyClass;
sendRequest(toyUrl);
}
//发送客户端的请求
function sendRequest(url) {
createXMLHttpRequest();
XMLHttpReq.open("get", url, true);
//指定响应函数
XMLHttpReq.onreadystatechange = handleResponse;
// 发送请求
XMLHttpReq.send(null);
}
//清除下拉列表中的值
function clearList()
{
var subList = document.getElementById("subClass");
while(subList.childNodes.length>0)
{
subList.removeChild(subList.childNodes[0]);
}
}
//把满足条件的商品信息添加到第二个下拉列表中
function addOption(top,sub)
{
var subList = document.getElementById("subClass");
var option = document.createElement("option");
option.appendChild(document.createTextNode(sub));
subList.appendChild(option);
}
//图片上传
//处理服务器响应结果
function handleupResponse() {
// 判断对象状态
if (XMLHttpReq.readyState == 4) {
// 信息已经成功返回,开始处理信息
if (XMLHttpReq.status == 200) {
var out = "";
var res = XMLHttpReq.responseXML;
var response = res.getElementsByTagName("upload")[0].firstChild.nodeValue;
}
}
}
//添加商品到购物车
function uploadImg()
{
var toys = document.getElementById("toys").value;
var toyUrl="/toysshop/servlet/FileUpload?toys="+toys;
sendupRequest(toyUrl);
}
//发送客户端的请求
function sendupRequest(url) {
createXMLHttpRequest();
XMLHttpReq.open("get", url, true);
//指定响应函数
XMLHttpReq.onreadystatechange = handleupResponse;
// 发送请求
XMLHttpReq.send(null);
}
function setupMsg(msg) {
mdiv = document.getElementById("showUpload");
if(msg.length == 2) {
sendupRequest("/toysshop/Admin/adminIndex.jsp?action=addtoys")
mdiv.innerHTML = "<font color='red'>上传图片成功</font>";
} else {
mdiv.innerHTML = "<font color='y'>no goods exities!</font>";
}
}