JQuery getJSON以另一种顺序工作,为什么?

时间:2012-08-11 17:51:49

标签: php jquery getjson

在这段代码中,我按顺序1,2,3,4,5发送数据......但是当它将这些数据添加到数据库时,它就像1,4,5,3,2或其他东西。

  for(var i = 1; i<=childs;i++){
        var u = $('#name'+i).val();
        var inf = $('#info'+i).val();
        var l = $('#location'+i).val();
        var b = $('#birth'+i).val();
        var d = $('#death'+i).val();
        var formData = "mode=add&number="+i+"&to="+t;

        if(u.length > 0){
            formData += "&username="+u;
        }else if(inf.length > 0){
            formData += "&info="+inf;
        }else if(l.length > 0){
            formData += "&location="+l;
        }else if(b.length > 0){
            formData += "&birth"+b;
        }else if(d.length > 0){
            formData += "&death"+d;
        }

        $.getJSON("content/child.php", formData, function(json){
          $.each(json, function(key, value){
          console.log(value); // in div i wrote ar1, ar2, ar3, ar4, ar5... but in database it addess how he wants ar1, ar3, ar4, ar5, ar2 let's say
                  });
        });
    }
});

4 个答案:

答案 0 :(得分:0)

正如您所发现的,JSON对象是无序的键值对集合。如果需要保留顺序,则使用键值对数组。

答案 1 :(得分:0)

您的for循环似乎比$.getJSON函数执行得更快,并且会导致意外结果。

您可以尝试$.when()实用程序功能,请尝试以下操作:

  

提供一种基于一个或多个对象执行回调函数的方法,通常是表示异步事件的Deferred对象。

var i = 0;
var len = childs;

function register() {
  i++;
  var u = $('#name'+i).val();
  var inf = $('#info'+i).val();
  var l = $('#location'+i).val();
  var b = $('#birth'+i).val();
  var d = $('#death'+i).val();
  var formData = "mode=add&number="+i+"&to="+t;

  if (u.length > 0){
    formData += "&username="+u;
  } else if(inf.length > 0){
    formData += "&info="+inf;
  } else if(l.length > 0){
    formData += "&location="+l;
  } else if(b.length > 0){
    formData += "&birth"+b;
  } else if(d.length > 0){
    formData += "&death"+d;
  }

  $.when($.getJSON("content/child.php", formData)).then(function(){
     if (i < len) {
        register()
     }
  })
}

答案 2 :(得分:0)

从jQuery 1.5延迟对象开始:

这可能会有所帮助

var result = [];
var xhr = [];

for (var i = 1; i <= childs; i++) {
    var u = $('#name' + i).val();
    var inf = $('#info' + i).val();
    var l = $('#location' + i).val();
    var b = $('#birth' + i).val();
    var d = $('#death' + i).val();
    var formData = "mode=add&number=" + i + "&to=" + t;

    if (u.length > 0) {
        formData += "&username=" + u;
    } else if (inf.length > 0) {
        formData += "&info=" + inf;
    } else if (l.length > 0) {
        formData += "&location=" + l;
    } else if (b.length > 0) {
        formData += "&birth" + b;
    } else if (d.length > 0) {
        formData += "&death" + d;
    }

    xhr.push($.getJSON("content/child.php", formData, function(json) {
        result.push(json);
    }));
}

$.when.apply($, xhr).done(function() {
    console.log(result);
});

答案 3 :(得分:0)

这个有效!!! ;)感谢大家! =)

   function register(i, len, to) {
      var u = $('#name'+i).val();
      var inf = $('#info'+i).val();
      var l = $('#location'+i).val();
      var b = $('#birth'+i).val();
      var d = $('#death'+i).val();
      var formData = "mode=add&number="+i+"&to="+to;

      if (u.length > 0){
        formData += "&username="+u;
      } else if(inf.length > 0){
        formData += "&info="+inf;
      } else if(l.length > 0){
        formData += "&location="+l;
      } else if(b.length > 0){
        formData += "&birth"+b;
      } else if(d.length > 0){
        formData += "&death"+d;
      }

      $.when($.getJSON("content/child.php", formData)).then(function(json){
         $.each(json, function(key, value){
            if(i == len){
                $('#mask').remove();
            } 
         });
         if (i < len) {
            i += 1;
            register(i, len, to)
         }
      })
    }
    $('.childAdd').live('click', function(){
    var childs = eval($('#childs').attr('count'));
    var to = eval($('#childs').attr('to'));

    var i = 1;
    var len = childs;

    register(i, len, to);
        });