Jquery 空数组时为";推;在$.post函数中使用
我想将“$.post”的结果“.push”放入数组中 我尝试了以下代码,但在中显示Jquery 空数组时为";推;在$.post函数中使用,jquery,Jquery,我想将“$.post”的结果“.push”放入数组中 我尝试了以下代码,但在中显示cats.push(数据)在$内不起作用。post功能,我可以用警报(数据)进行确认该数据变量不是空的,但最后“cats”数组是空的 $('[id^="mul-"]').click(function () { cats = []; $(this).toggleClass('selected'); $(".
cats.push(数据)
在$内不起作用。post
功能,我可以用警报(数据)进行确认
该数据变量不是空的,但最后“cats”数组是空的
$('[id^="mul-"]').click(function () {
cats = [];
$(this).toggleClass('selected');
$(".selected").each(function(){
$.post( "/index.php?do=getcatname&id=" + this.id.slice().replace(/mul-/,''), function( data ) {
cats.push(data);
alert(data);
});
});
ms = cats.join(",");
alert(ms);
});
AJAX中的第一个字母
A
表示异步。这意味着$.post
方法立即返回,成功回调可能会在很久以后调用。在这里,您似乎正在执行多个AJAX请求,在每个AJAX请求的成功回调中,您似乎在向数组添加一些元素。因此,直到最后一个AJAX请求执行完毕,您才能使用此数组的结果
您可以使用jQuery 1.5中引入的延迟执行来聚合多个AJAX调用的结果:
$('[id^="mul-"]').click(function () {
$(this).toggleClass('selected');
// get an array of the ids that will be used for each AJAX call
var ids = $(".selected").map(function() {
return this.id.slice().replace(/mul-/,'');
});
// declare the variable that will hold the result
var categories = [];
var ajaxCall = function(i) {
return $.ajax({
url: '/index.php',
type: 'POST',
data: { do: 'getcatname', id: i }
}).done(function (res) {
categories.push(res);
});
};
$.when.apply($, ids.map(function(i) {
return ajaxCall(i);
})).then(function() {
// Here you could use the categories array:
var result = categories.join(', ');
alert(result);
});
});
尽管如此,您应该记住,就性能而言,发出多个小AJAX请求比发出一个更大的AJAX请求更糟糕。这意味着您应该修改index.php
脚本,以便它能够将id
列表作为参数(而不是单个id)并返回相应类别的列表。然后在.click处理程序中,您将构建此ID列表,并向服务器发送一个AJAX请求。这基本上是将逻辑推送到服务器上,这将比当前的体系结构方法更快
因此,下面是代码的一个改进版本的外观:
$('[id^="mul-"]').click(function () {
$(this).toggleClass('selected');
// get an array of the ids that will be used for each AJAX call
var ids = $(".selected").map(function() {
return this.id.slice().replace(/mul-/,'');
});
$.ajax({
url: '/index.php',
type: 'POST',
data: { do: 'getcatnames', ids: ids },
success: function(categoryNames) {
alert(categoryNames.join(', '));
}
});
});
请注意,我们是如何将整个ID列表发送到sevrer端脚本的,该脚本将负责以JSON数组的形式返回相应类别名称的列表:
['cat1','cat2','cat3',…]
AJAX中的第一个字母a
表示异步。这意味着$.post
方法立即返回,成功回调可能会在很久以后调用。在这里,您似乎正在执行多个AJAX请求,在每个AJAX请求的成功回调中,您似乎在向数组添加一些元素。因此,直到最后一个AJAX请求执行完毕,您才能使用此数组的结果
您可以使用jQuery 1.5中引入的延迟执行来聚合多个AJAX调用的结果:
$('[id^="mul-"]').click(function () {
$(this).toggleClass('selected');
// get an array of the ids that will be used for each AJAX call
var ids = $(".selected").map(function() {
return this.id.slice().replace(/mul-/,'');
});
// declare the variable that will hold the result
var categories = [];
var ajaxCall = function(i) {
return $.ajax({
url: '/index.php',
type: 'POST',
data: { do: 'getcatname', id: i }
}).done(function (res) {
categories.push(res);
});
};
$.when.apply($, ids.map(function(i) {
return ajaxCall(i);
})).then(function() {
// Here you could use the categories array:
var result = categories.join(', ');
alert(result);
});
});
尽管如此,您应该记住,就性能而言,发出多个小AJAX请求比发出一个更大的AJAX请求更糟糕。这意味着您应该修改index.php
脚本,以便它能够将id
列表作为参数(而不是单个id)并返回相应类别的列表。然后在.click处理程序中,您将构建此ID列表,并向服务器发送一个AJAX请求。这基本上是将逻辑推送到服务器上,这将比当前的体系结构方法更快
因此,下面是代码的一个改进版本的外观:
$('[id^="mul-"]').click(function () {
$(this).toggleClass('selected');
// get an array of the ids that will be used for each AJAX call
var ids = $(".selected").map(function() {
return this.id.slice().replace(/mul-/,'');
});
$.ajax({
url: '/index.php',
type: 'POST',
data: { do: 'getcatnames', ids: ids },
success: function(categoryNames) {
alert(categoryNames.join(', '));
}
});
});
请注意,我们是如何将整个ID列表发送到您的sevrer端脚本的,该脚本将负责以JSON数组的形式返回相应类别名称的列表:
['cat1'、'cat2'、'cat3'、…]
Ajax是异步的。为了澄清上述内容,您需要在回调中执行mscategory
赋值。Ajax是异步的。为了澄清上述问题,您需要在回调中执行mscategory
赋值。