Javascript 我们如何为3个函数重用jqueryajax方法?
我使用$jquery ajax方法来提取数据并在3个字段中显示,我对3个不同的文本框使用了相同的ajax方法,to、CC、Bcc,我想使用1个ajax方法来提取数据,这里是tagSource:一个调用ajax方法的函数Javascript 我们如何为3个函数重用jqueryajax方法?,javascript,jquery,ajax,tag-it,Javascript,Jquery,Ajax,Tag It,我使用$jquery ajax方法来提取数据并在3个字段中显示,我对3个不同的文本框使用了相同的ajax方法,to、CC、Bcc,我想使用1个ajax方法来提取数据,这里是tagSource:一个调用ajax方法的函数 jQuery(document).ready(function () { jQuery("#totags").tagit({ allowSpaces: true, tagSource: fun
jQuery(document).ready(function () {
jQuery("#totags").tagit({
allowSpaces: true,
tagSource: function (request, response) {
$.ajax({
type: "POST",
url: "Blog.aspx/GetName",
data: "{'match': '" + request.term + "'}",
dataType: "json",
contentType: "application/json",
success: function (data) {
console.log(data);
if (data.d != null) {
response($.map(data.d, function (item) {
return {
label: item.Name,
value: item.id,
};
}));
}
}
});
},
});
jQuery("#cctags").tagit({
allowSpaces: true,
tagSource: function (request, response) {
$.ajax({
type: "POST",
url: "Blog.aspx/GetName",
data: "{'match': '" + request.term + "'}",
dataType: "json",
contentType: "application/json",
success: function (data) {
console.log(data);
if (data.d != null) {
response($.map(data.d, function (item) {
return {
label: item.Name,
value: item.id,
};
}));
}
}
});
},
});
jQuery("#bcctags").tagit({
allowSpaces: true,
tagSource: function (request, response) {
$.ajax({
type: "POST",
url: "Blog.aspx/GetName",
data: "{'match': '" + request.term + "'}",
dataType: "json",
contentType: "application/json",
success: function (data) {
console.log(data);
if (data.d != null) {
response($.map(data.d, function (item) {
return {
label: item.Name,
value: item.id,
};
}));
}
}
});
},
});
});
您可以使用jQuery链接选择器:
jQuery(document).ready(function () {
jQuery("#totags, #cctags, #bcctags").tagit({
// ..
});
});
您可以使用jQuery链接选择器:
jQuery(document).ready(function () {
jQuery("#totags, #cctags, #bcctags").tagit({
// ..
});
});
一般情况下,将其分配给var:
var ajaxcall=function(request, response) {
$.ajax({
type: "POST",
url: "Blog.aspx/GetName",
data: "{'match': '" + request.term + "'}",
dataType: "json",
contentType: "application/json",
success: function(data) {
console.log(data);
if (data.d != null) {
response($.map(data.d, function(item) {
return {
label: item.Name,
value: item.id,
};
}));
}
}
});
};
jQuery("#totags").tagit({
allowSpaces: true,
tagSource: ajaxcall
});
jQuery("#cctags").tagit({
allowSpaces: true,
tagSource: ajaxcall
});
jQuery("#bcctags").tagit({
allowSpaces: true,
tagSource: ajaxcall
});
在特定情况下,使用Florent回答的链。在一般情况下,将其分配给var:
var ajaxcall=function(request, response) {
$.ajax({
type: "POST",
url: "Blog.aspx/GetName",
data: "{'match': '" + request.term + "'}",
dataType: "json",
contentType: "application/json",
success: function(data) {
console.log(data);
if (data.d != null) {
response($.map(data.d, function(item) {
return {
label: item.Name,
value: item.id,
};
}));
}
}
});
};
jQuery("#totags").tagit({
allowSpaces: true,
tagSource: ajaxcall
});
jQuery("#cctags").tagit({
allowSpaces: true,
tagSource: ajaxcall
});
jQuery("#bcctags").tagit({
allowSpaces: true,
tagSource: ajaxcall
});
在特定情况下,请使用Florent回答的链条