Ajax 加载函数(操作类)

前端之家收集整理的这篇文章主要介绍了Ajax 加载函数(操作类)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//ajax加载函数(get请求)
function callback1(response)
{
	alert(response);
}
function get_json_str(name)
{
	var tag = document.getElementsByName(name);
	var value="";
	for(var i =0;i<tag.length;i++)
		{
			if(tag[i].checked == true)
				{
					value = tag[i].value;
				}
		}
	id = document.getElementById('usr_id').innerHTML;
	txt = '{"usr_id":'+id+',"act_id":'+name+',"have":"'+value+'"}';
	return txt;
}
function myclick(n)
{
	var json_str="[";
	switch(n){
	case 1:
		{
			txt = get_json_str("101");
			json_str += txt;
			json_str += ",";
			txt = get_json_str("102");
			json_str += txt;
			json_str += ",";
			txt = get_json_str("103");
			json_str += txt;
			break;
		}
	case 2:
	{
		txt = get_json_str("201");
		json_str += txt;
		break;
	}
	case 3:
	{
		txt = get_json_str("301");
		json_str += txt;
		json_str += ",";
		txt = get_json_str("302");
		json_str += txt;

		break;
	}
	case 4:
	{
		txt = get_json_str("401");
		json_str += txt;
		json_str += ",";
		txt = get_json_str("402");
		json_str += txt;
		json_str += ",";

		txt = get_json_str("403");
		json_str += txt;
		break;
	}
	case 5:
	{
		txt = get_json_str("501");
		json_str += txt;
		json_str += ",";
		txt = get_json_str("502");
		json_str += txt;
		break;
	}
	case 6:
	{

		txt = get_json_str("601");
		json_str += txt;
		json_str += ",";

		txt = get_json_str("602");
		json_str += txt;
		break;
	}
	case 7:
	{
		txt = get_json_str("701");
		json_str += txt;
		break;
	}
	}
	json_str += "]";
	//alert(json_str);
	var xmlhttp = null;
	xmlhttp = GetAjaxObj(callback1);
	xmlhttp.open("GET","2.PHP?jsons="+json_str,true);
	xmlhttp.send();	
}
function load(){
	document.getElementById("content1").style.display = "block";
	doload(1);
}
function callback(response)
{
	//alert(document.getElementById('usr_id').innerHTML );
	var objs = eval("(" + response + ")");
	for(var i=0;i<objs.length;i++){
		var name = objs[i][1];
		var tags = document.getElementsByName(name);
		var priv = objs[i][2];
		if(priv == "Y")
			{
			tags[0].checked = true;
			tags[1].checked = false;					
			}else
			{
			tags[1].checked = true;
			tags[0].checked = false;
			}
	}
}
function GetAjaxObj(callback){
	if(window.XMLHttpRequest){
		// code for IE7+,Firefox,Chrome,Opera,Safari
		xmlhttp=new XMLHttpRequest();
	}else{
		// code for IE6,IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function(){
		//当请求状态为4时,响应状态为200时
		if(xmlhttp.readyState==4 && xmlhttp.status==200){
			txt = xmlhttp.responseText;
			callback(txt);
		}
	}
	return xmlhttp;
}

function doload(id){
	document.getElementById('usr_id').innerHTML = id;
	var xmlhttp = null;
	xmlhttp = GetAjaxObj(callback);
	xmlhttp.open("GET","1.PHP?id="+id,true);
	xmlhttp.send();
}
原文链接:/ajax/165563.html

猜你在找的Ajax相关文章