$(document).ready(function(){
/* Asynchronous request, load JSON data*/ /PeopleList.aspx",
function(data){
/* Traverse the request results*/
$.each(data,
function(index, p){
var html = "
" p.name " | " p.age " | " (p.isMale ? "male" : " female") " |
"
$("#peoples>tbody").append(html);
});
});
}) ;
<%@ Page Language="C#" %>
[{
"name" : "David li",
"age" : 61,
" isMale" : true
},{
"name" : "Michael Clinton",
"age" : 53,
"isMale" : true
},{
"name " : "Brook Ann",
"age" : 23,
"isMale" : false
},{
"name" : "Mary Johnson",
"age" : 35 ,
"isMale" : false
},{
"name" : "Elizabeth Jones",
"age" : 33,
"isMale" : false
},{
"name" : "James Smith",
"age" : 25,
"isMale" : true
}]
8:$.getScript()方法
var xmlReq = $.getScript(url, [callback]);
getScript.js
$(document).ready(function(){
$("#input").click(function(){
$.getScript("Test.js", function(data){
showMsg();
});
});
});
Test.js
function showMsg(){
alert("This is Message");
}
9:序列化表单数据
jQuery为了解决参数很多的问题,提供了序列化的方法简化对表单数据的收集和格式化。
serialize.js
$(document).ready(function(){
$("button[name='btnSubmit']").click(function(){
$.post("http: //localhost:2154/Web/Register.aspx",
$("form").serialize(), //Serialize form data
function(data){
$("table tbody" ).append("
" data " |
");
});
});
});
Register.aspx
<%@ Page Language="C#" %>
Username:<%= Request["username"] %>
Age :<%= Request["age"] %>
IsMale:<%= Request["isMale"]%>
Email:<%= Request["email"]%>
Details:<%= Request["details"]%>
10: serializeArray() method
This method serializes the page form into a JSON structured object, which is in the form of a collection of "key/value" pairs Encapsulates all element values in the form.
Note here that this method returns a JSON object, not a JSON string
The structure of the JSON object is as follows:
" , "value": "value2"},
);
var textName = jsonData[0].name;
var textValue = jsonData[0].value;
11: Set global Ajax default options
In applications, a large number of Ajax methods are often written to implement various functions. Each time, a large number of parameters are set in the $.ajax() method, which is very inconvenient. jQuery provides $.ajaxSetup () method, you can set global Ajax default parameter items.
$.ajaxSetup([options]);Copy code
type: 'POST',
global: false, //Disable triggering of global events
dataType: 'json',
error: function(xhr, textStatus, errorThrown) {
; 🎜>
ajaxComplete(callback); //This event is triggered when the request is completed
ajaxError(callback); //This event is triggered when an error occurs in the request
ajaxSend(callback); //The request is sent This event is triggered before
ajaxStart(callback); //This event is triggered when the request starts
ajaxStop(callback); //This event is triggered when the request ends
ajaxSuccess(callback); //Triggered when the request is successful The event handler of the
ajaxStart() method and ajaxStop method is a parameterless function, and the rest can have 3 parameters. The syntax format is as follows:
Copy the code The code is as follows:
Function (Event, XHR, SETTINGS) {
EVENT is a triggered event object
XHR is the XMLHTTPRequest object
Settings are Ajax request configuration parameters
AjaxGlobalEvent.js
$(document).ready(function(){
$("#show").ajaxStart(function(){
$(this).append("
Run ajaxStart
");
});
$("#show").ajaxStop(function(){
$(this).append("
Run ajaxStop
");
});
$("#show").ajaxComplete(function(){
$(this).append("
Run ajaxComplete
");
});
$("#show").ajaxError(function(){
$(this).append("
Run ajaxError
");
});
$("#show").ajaxSend(function(){
$(this).append("
Run ajaxSend
");
});
$("#show").ajaxSuccess(function(){
$(this).append("
Run ajaxSuccess
");
});
$("button[name='btnLoad']").click(function(){
$.get("http://www.sohu.com");
});
});