0

get json 関数によって要素にデータを追加するこのスクリプトがあります。

 $(document).ready(function() {
        ADD.Listitem.get();
        });

基本的に、データなどを含む一連のhtmlタグを追加します。私が抱えている問題は次のとおりです。

 $(document).ready(function() {
    ADD.Listitem.get();

    var arr = [];
    $(".Listitem-section-item-title").each(function() {
        arr.push($(this.text()));
    });
 });

-

get: function(web) {
            AST.Utils.JSON.get("/_vti_bin/AST/ListItem/ListitemService.svc/GetListItem", null, AST.Listitem.renderListitem);
        },
renderListitem: function(data) {
            $("#Listitem-template").tmpl(data["ListItemResults"]).prependTo(".ListItem-section-template");
    }

そしてここにjson getがあります:

ADD.Utils.JSON.get = function (url, data, onSuccess) {
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        async: true,
        url: url,
        data: data,
        cache: false,
        dataType: "json",
        success: onSuccess,
        error: ADD.Utils.JSON.error,
        converters: { "text json": ADD.Utils.JSON.deserialize }
    });
}

getメソッドがセレクターのレンダリングを終了していないため、各ループの配列は実行されていないListitem-section-item-titleため、セレクターを見つけることができません。

これに対する良い解決策はありますか?

4

3 に答える 3

1

によって与えられた約束を返すように関数を変更できます$.ajax

ADD.Utils.JSON.get = function (url, data) {
    return $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        async: true,
        url: url,
        data: data,
        cache: false,
        dataType: "json",
        converters: { "text json": ADD.Utils.JSON.deserialize }
    }).fail(ADD.Utils.JSON.error);
}

get: function(web) {
    return AST.Utils.JSON.get("/_vti_bin/AST/ListItem/ListitemService.svc/GetListItem", null).done(AST.Listitem.renderListitem);
},

できるように

 $(document).ready(function() {
    ADD.Listitems.get().done(function(){
        var arr = [];
        $(".Listitem-section-item-title").each(function() {
            arr.push($(this.text()));
        });
    });
 });
于 2013-10-08T09:08:58.070 に答える
0

折り返し電話:

$(document).ready(function() {
 ADD.Listitem.get(url,data,function(){
   var arr = [];
   $(".Listitem-section-item-title").each(function() {
     arr.push($(this.text()));
   });
 });
});

コールバックなし:

get メソッドでコールバックを取得したり、promise を返したりできない場合は、いつ完了したかを確認するのが最善の方法だと思います。

$(document).ready(function() {

  ADD.Listitem.get();

  var timer = setInterval(function(){
    if ($("#thingWhichShouldExist").length>0){
      var arr = [];
      $(".Listitem-section-item-title").each(function() {
        arr.push($(this.text()));
      });
    clearInterval(timer);
    }
  },50);
});
于 2013-10-08T09:07:33.863 に答える
-1

値を取得し、成功したら、値を配列にプッシュする関数を呼び出します。

また、arr.push($(this.text()));する必要がありますarr.push($(this).text());

于 2013-10-08T09:13:38.443 に答える