Javascript 追加订单-jQuery
我试图让字符串按显示方式排序(红色、蓝色、橙色、黑色)。出于某种原因,它会随机追加订单。例如:它将输出(蓝色、橙色、红色、黑色)。任何帮助都会很好。谢谢Javascript 追加订单-jQuery,javascript,jquery,Javascript,Jquery,我试图让字符串按显示方式排序(红色、蓝色、橙色、黑色)。出于某种原因,它会随机追加订单。例如:它将输出(蓝色、橙色、红色、黑色)。任何帮助都会很好。谢谢 var tCookie = "red,blue,orange,black"; var Cookies = tCookie.split(','); if (Cookies) { for (var i = 1; i <= Cookies.length; i++) { var dataString = "TabId=
var tCookie = "red,blue,orange,black";
var Cookies = tCookie.split(',');
if (Cookies) {
for (var i = 1; i <= Cookies.length; i++) {
var dataString = "TabId="+Cookies[i]+"";
$.ajax({
type: "POST",
url: "includes/tab.php",
data: dataString,
cache: false,
success: function(html){
$("#Dynamic_Tab").append(html).children(':last').hide().fadeIn("fast");
}
});
}
}
var tCookie=“红、蓝、橙、黑”;
var Cookies=tCookie.split(',');
如果(Cookies){
对于(var i=1;i您可以有一个请求和响应的列表,并在所有操作完成后开始追加,以便顺序始终正确:
var deferreds = [],
results = [];
for (var i = 1; i <= Cookies.length; i++) {
(function(i) { // to freeze i
var dataString = "TabId="+Cookies[i]+"";
deferreds.push($.ajax({
type: "POST",
url: "includes/tab.php",
data: dataString,
cache: false,
success: function(html){
results[i] = html; // insert at the synchronous position
}
}));
})(i);
}
$.when.apply($, deferreds).then(function() {
$.each(results, function(i, html) {
$("#Dynamic_Tab").append(html).children(':last').hide().fadeIn("fast");
});
});
var延期=[],
结果=[];
对于(var i=1;i,您可以在这里使用延迟对象仅在所有AJAX请求返回后追加HTML:
//create array to store XHR objects that will resolve when the AJAX requests return
//also create an object to store the AJAX responses
var jqXHRs = [],
responses = {};
//iterate through each of the cookie indexes
$.each(cookies, function (index, value) {
//create the dataString and cache the value of this index so it can be used in the success callback for the AJAX request associated with this index
var dataString = "TabId=" + value,
thisValue = value;
//store an empty string in the output variable for the current index, this keeps it's place in-line
responses[thisValue] = '';
//do the AJAX request and store it's XHR object in the array with the rest
jqXHRs[jqXHRs.length] = $.ajax({
type : "POST",
url : "includes/tab.php",
data : dataString,
cache : false,
success : function (html) {
//now that the AJAX request has returned successfully, add the returned HTML to the output variable for this index
responses[thisValue] = html;
}
});
});
//wait for all of the XHR objects to resolve then add all the HTML to the DOM
$.when(jqXHRs).then(function () {
//all of the AJAX requests have come back and you can now add stuff to the DOM
var $element = $("#Dynamic_Tab");
$.each(responses, function (index, value) {
$element.append(value).children(':last').hide().delay(index * 250).fadeIn(250);
}
});
.delay()
是这样的,每一个新行都会依次淡入。异步:“false”不应该出现在示例中。我的fault@Joe:我看到您现在删除了async
部分,但这意味着它仍然是true
,因为这是默认值。对。我正在尝试在不使用async的情况下解决此问题,因为它在显示之前加载了整个脚本。有没有办法不使用async来完成此操作?好的,谢谢pimvdb。我会处理这个问题,看看会发生什么。请显示您的HTML响应Hanks Jasper。我想我明白了