简单的ajax封装

//ajax发送post请求返回json数据

functionrequestJSON(params){

params.dataType='json';

sendPost(params);

}

//ajax发送post请求返回string字符串

functionrequestString(params){

params.dataType='text';

sendPost(params);

}

//ajax发送post请求

functionsendPost(params){

params.type='post';

sendRequest(params);

}

//ajax发送get请求

functionrequest(params){

params.type='get';

sendRequest(params);

}

//ajax请求封装

functionsendRequest(params){

params=$.extend({},{async:true},params);

$.ajax({

url:params.url,

data:params.data,

type:params.type,

dataType:params.dataType,

success:params.success,

async:params.async,

beforeSend:function(){

if(typeof(params.before)=='function'){

params.before();

}

else{

//页面数据请求提示loading效果

jQuery('body').Loading({marginTop:'-300px;'});

}

},

error:function(){

jQuery('body').hideLoading();

if(typeof(params.error)=='function')params.error();

},

complete:function(){

if(typeof(params.complete)=='function'){

params.complete();

}

else{

//请求完成处理

jQuery('body').hideLoading();

}

}

});

}

////---------------------------------@发布---------------------------//

functiontextChange(options){

varopt=$.extend({

Content:''

},options);

varobjId=opt.Content;

varobj=$("#"+objId);

obj.keydown(function(evt){

vark=window.event?evt.keyCode:evt.which;

varisChrome=window.navigator.userAgent.indexOf("Chrome")!==-1;

if(isChrome&&k==16){

k=50;

}

//为@键值时

//这里监听输入框的keyup事件

//不为空&&不为上箭头或下箭头或回车

if(k==50&&evt.shiftKey){

buildTip(obj,'');

}

elseif(k==13||k==38||k==40){//回车

if($('#searchresultul').length>0){

if(k==38){//上箭头

$('#searchresulta.searchHover').parent().prev().find("a").addClass("searchHover");

$('#searchresulta.searchHover').parent().next().find("a").removeClass("searchHover");

}elseif(k==40){//下箭头

$('#searchresulta.searchHover').parent().next().find("a").addClass("searchHover");

$('#searchresulta.searchHover').parent().prev().find("a").removeClass("searchHover");

}elseif(k==13){//回车

varselContent=$('#searchresulta.searchHover').attr('rname');

if(selContent==""||selContent==undefined)return;

varcontent=selContent+'';

//移除textarea里面@后面多余数据

varpointIndex=getPositionForTextArea(obj[0]);

varsearchname="";

varatIndex=obj.val().substring(0,pointIndex).lastIndexOf("@",atIndex);

obj.val(obj.val().substring(0,atIndex+1)+obj.val().substring(pointIndex));

varnewIndex=getPositionForTextArea(obj[0]);

setCursorPosition(obj[0],newIndex-(newIndex-atIndex-1));

resetPostion(content,obj);

$("#searchresult").remove();

}

evt.returnValue=false;

returnfalse;

}

}

else{

//$("#searchresult").remove();

}

});

obj.keyup(function(evt){

vark=window.event?evt.keyCode:evt.which;

if(k!=13&&k!=38&&k!=40){

if($('#searchresultul').length>0){

varpointIndex=getPositionForTextArea(obj[0]);

varsearchname="";

varatIndex=obj.val().substring(0,pointIndex).lastIndexOf("@",atIndex);

searchName=obj.val().substring(atIndex+1,pointIndex);

buildTip(obj,searchName);

}

}

});

}

//构建提示信息

functionbuildTip(obj,serachname){

$.ajax({

type:'post',

url:'',

data:'',

dataType:'json',

success:function(data){

varobjData=data.list;

if(objData.length>0){

if($("#searchresult").length>0)

$("#searchresult").remove();

varlayer="";

layer="<divid='searchresult'><ul>";

$.each(objData,function(idx,item){

layer+="<li><ahref='javascript:void(0);'></a></li>";

});

layer+="</ul></div>";

//将结果添加到div中

obj.after($(layer));

$("#searchresulta:first").addClass("searchHover");

$("#searchresult").css("display","");

//鼠标点击事件

$("#searchresulta").click(function(){

varcontent=$(this).attr('rname')+'';

resetPostion(content,obj);

$("#searchresult").empty();

$("#searchresult").css("display","none");

});

$("#searchresulta").each(function(){

$(this).mouseover(function(e){

$("#searchresulta").removeClass("searchHover");

$(this).addClass("searchHover");

});

});

//evt.stopPropagation();

//处理js事件冒泡问题

$('body').bind("click",function(e){

$("#searchresult").remove();

e.stopPropagation();

});

$("#searchresult").bind("click",function(e){

e.stopPropagation();

});

}else{

$("#searchresult").remove();

}

}

});

}

varcpos=0;

functionresetPostion(content,target){

vartc=target[0];

if(document.selection){//ie

target.bind("clickkeyup",function(e){//点击或键盘动作时设置光标值

e.stopPropagation();

cpos=getPositionForTextArea(tc);

});

}

vartclen=target.val().length;

varpos=0;

if(typeofdocument.selection!="undefined"){//IE

target.focus();

//setCursorPosition(tc,cpos);//设置焦点

document.selection.createRange().text=content;

//计算光标位置

pos=getPositionForTextArea(tc);

}else{//火狐

//计算光标位置

pos=tc.selectionStart+content.length;

target.val(target.val().substr(0,tc.selectionStart)+content+target.val().substring(tc.selectionStart,tclen));

}

cpos=pos;

setCursorPosition(tc,pos);//设置焦点

}

//textarea设置光标位置

functionsetCursorPosition(ctrl,pos){

if(ctrl.setSelectionRange){

ctrl.focus();

ctrl.setSelectionRange(pos,pos);

}elseif(ctrl.createTextRange){//IESupport

varrange=ctrl.createTextRange();

range.collapse(true);

range.moveEnd('character',pos);

range.moveStart('character',pos);

range.select();

}

}

//获取多行文本框光标位置

functiongetPositionForTextArea(obj,content){

varCaretPos=-1;

if(typeofdocument.selection!="undefined"){//IE

varSel=document.selection.createRange();

varSel2=Sel.duplicate();

Sel2.moveToElementText(obj);

while(Sel2.inRange(Sel)){

Sel2.moveStart('character');

CaretPos++;

}

}

else{

if(content==undefined)

content="";

CaretPos=obj.selectionStart+content.length;

}

returnCaretPos;

}

相关推荐