0

在这段代码中,我以 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

4 回答 4

0

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

于 2012-08-11T17:57:35.263 回答
0

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

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

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

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()
     }
  })
}
于 2012-08-11T18:01:23.853 回答
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);
        });
于 2012-08-12T16:50:28.893 回答
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);
});
于 2012-08-11T19:07:13.413 回答