jquery ajax小感
凡是做网页项目的一些战豪们都经常会遇到这样的问题:将actionController操作的结果返回到你所要显示的页面当中,并且是以异步的形式显示,
这个时候ajax就闪亮登场了。
举个小例子来说名个人感受吧:
$("#CollectedByAgency").click(function () {
if ($(this).prop("checked") == true) {
$("#CollectedByAgency").prop('checked', true);
$('div[soGuid]').each(function () {
$.ajax({
url: '/WebApi/GetCollection/?id=' + $(this).attr("soGuid") + "&collectType=true",
soGuid: $(this).attr("soGuid"),
//table: $(this).parent().parent().parent().parent(),
table:$(this).parents().filter("table.serviceItem"),
type: 'GET',
success: function (data) {
if (data == "NO") {
$(this.table).hide();
} else {
$("#" + this.soGuid).div.html(data);
}
},
error: function (data) {
alert("错误数据");
}
});
});
} else {
$('div[soGuid]').each(function () {
$(".serviceItem").show();
$(this).load('/WebApi/GetCollection/?id=' + $(this).attr("soGuid") + "&collectType=false");
});
$("#CollectedByAgency").prop('checked', false);
}
}
);
1.这里的$.ajax{table:}可以自定义属性,可以把你想要传递的参数传递过来。success:返回回来的this对象是ajax,可以用
for(item in this)
{
alert(item);
}的方法来进行显示一下this的属性,你就可以知道对象是谁了。
2.checkbox 的checked属性,用prop,不要用attr来判断。