jquery中各个ajax实例操作

时间:2022-05-06
本文章向大家介绍jquery中各个ajax实例操作,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>jquery_ajax.html</title>
		<script type="text/javascript" src="jquery.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				//jquery.ajax()
				$("#button1").click(function(){
					htmlOjbect = $.ajax({url:"/www.7789.com/testfile/test.php?c=3&t=5", async:false});//不要忘记{}
					$("#getText1").html(htmlOjbect.responseText);
				});

				//ajaxSend()
				$("#sendDiv").ajaxSend(function(e, xhr, opt){
					$(this).html("正在请求……"+opt.url);//此处的url就是下面的load地址
				});
				$("#button2").click(function(){
					$("#getText2").load("/www.7789.com/testfile/test.php?c=2&t=5");
				});

				//get()
				$(".button3").click(function(){
					$.get("/www.7789.com/testfile/test.php?c=5&t=5",function(result){
						$(".getText3").html(result);
					});
				});

				//get() method2
				$(".button4").click(function(){
					$.get("/www.7789.com/testfile/test.php",{c:'2', t:'0'},function(result){
						$(".getText4").html(result);
					});
				});

				//serializeArray()
				$("#formButton").click(function(){
					x = $("form").serializeArray();
					$.each(x, function(i, field){
						$("#results").append(field.name + ":" + field.value + "t");//里面的name和value分别对应input标签
					});
				});
			});
		</script>
	</head>
	<body>
		<div>
			<button id="button1">ajax异步获取内容</button>
			<div id="getText1"></div>
		</div>

		<div id="sendDiv">
			<button id="button2">ajaxSend异步获取内容</button>
			<div id="getText2"></div>
		</div>

		<div>
			<button class="button3">get异步获取内容1</button>
			<div class="getText3"></div>
		</div>

		<div>
			<button class="button4">get异步获取内容2</button>
			<div class="getText4"></div>
		</div>
		<div>
			<form action="">
				FirstName:<input type="text" name="firstName" value="Bill"/><br/>
				LastName:<input type="text" name="lastName" value="Gates"/><br/>
			</form>
			<button type="button" id="formButton">序列号表单</button>
			<div id="results" value="contents show..."></div>
		</div>

		<div>
			<span id="evalText1"></span>
			<span id="evalText2"></span>
		</div>
		<script type="text/javascript">
		//function eval test
			eval("x=10;y=20;document.write('x*y='+x*y);");
			document.write("<br/>2+12="+eval("2+12"));
			var x=10;
			document.write("<br/>x+17="+eval(x+17));
			//要注意txt里面的格式,很复杂的,最后还要少个‘才能正常运行
			var txt = '{"employees": ['
				+'{"firstName":"Bill","lastName":"Gates"},'
				+'{"firstName":"George","lastName":"Bush"},'
				+'{"firstName":"Thomas","lastName":"Yang"}]}';
			var obj = eval("("+txt+")");
			document.getElementById("evalText1").innerHTML = obj.employees[1].firstName;
			document.getElementById("evalText2").innerHTML = obj.employees[1].lastName;
		</script>
	</body>
</html>