日期:2014-05-16  浏览次数:20452 次

请问如何多个function共用一个ajax
在jquery下我想多个function共用一个ajax
$(document).ready(function(){
$("#reg").bind("click",enter);
$("#del").bind("click",del);
function enter(){
name = $("#name").val();
password=$("#password").val();
password1=$("#password1").val();
if(password==password1){
param={"name":name,"password":password,"act":"insert"};
}else{
alert("对不起,两次密码不一致");
return false;
}
$.ajax({
contentType:"application/x-www-form-urlencoded; charset=utf-8", 
type:'post',
url:'reg.php',
data:param,
async:false,
success: function(msg){
alert(msg);}
});
};
function del(){
name =$("#delname").val();
password=$("#delpassword").val();
param={"name":name,"password":password,"act":"delete"};
$.ajax({
contentType:"application/x-www-form-urlencoded; charset=utf-8", 
type:'post',
url:'reg.php',
data:param,
async:false,
success: function(msg){
alert(msg);}
});
}
});



请问如何实现多个function共用一个ajax呢? 每个function都要写个ajax也太不简洁了

------解决方案--------------------
引用:
是这样写吗?
$(document).ready(function(){
$("#reg").bind("click",enter);
$("#del").bind("click",del);
function enter(){
name = $("#name").val();
password=$("#password").val();
password1=$("#password1").val();
if(password==password1){
param={"name":name,"password":password,"act":"insert"};
}else{
alert("对不起,两次密码不一致");
return false;
}
ajaxRequest(param);
};
function del(){
name =$("#delname").val();
password=$("#delpassword").val();
param={"name":name,"password":password,"act":"delete"};
}
ajaxRequest(param);
});

function ajaxRequest(param){
$.ajax({
contentType:"application/x-www-form-urlencoded; charset=utf-8", 
type:'post',
url:'reg.php',
data:param,
async:false,
success: function(msg){
alert(msg);}
});
}

是的
------解决方案--------------------
        function del() {
            name = $("#delname").val();
            password = $("#delpassword").val();
            param = { "name": name, "password": password, "act": "delete" };
        ajaxRequest(param);
        }
     ajaxRequest(param); 这句发错位置了