无法通过带有JSON响应的JQuery Mobile生成动态列表视图?

时间:2013-09-04 05:29:17

标签: javascript android jquery json cordova

这是我的Json响应格式: -

{"Value":[{"ID":"1","productID":"10","subsID":"9","MinPrice":"500","MaxPrice":"50000","Subs":"xyz100"}]}

这是我的.js代码: -

function productURL(url) {
    alert(url);
    $.ajax({
        url: url,
        type : "GET",
        dataType : "json",
        contentType: "application/json",
        async : false,
        success : function(msg) {
            var respPrice = msg.Value;
            $.each(respPrice, function(index, value) {
                if(value.VarietyID == 0) {
                    alert('No product subs list found');
                } else {

                    subsId = value.subsID;
                    substance = value.subs;
                    minPrice = value.MinPrice;
                    maxPrice = value.MaxPrice;
                    respPrice += '<li class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-last-child ui-btn-up-e">' + substance + minPrice + '-' + maxPrice + '</li>';
                    };

                    }           
                });
             $('#productList').append(respPrice).listview('refresh');

                },
            error : function(e) {
                console.log(e.message);
                alert('Error Occoured');
            }
        });
}

我需要做些什么修改才能正确生成listview?任何帮助都会受到极大关注。

2 个答案:

答案 0 :(得分:1)

为什么不这样做?

$.each(respPrice.Value, function(index, value) {
      if(respPrice.VarietyID == 0) {
         alert('No product subs list found');
      } else {

       subsId = value.subsID;
       substance = value.Subs;
       minPrice = value.MinPrice;
       maxPrice = value.MaxPrice;       
       $respPrice = '<li class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-last-child ui-btn-up-e">' + substance + minPrice + '-' + maxPrice + '</li>';

       // append here
       $('#productList').append($respPrice);
     }               
 });

 // and when all done, after $.each() refresh the listview
 $('#productList').listview('refresh');

这是一个小提琴:http://jsfiddle.net/REthD/19/

答案 1 :(得分:0)

更改

success : function(msg) {
            var respPrice = "";
            $.each(respPrice, function(index, value) {
                if(value.VarietyID == 0) {
                    alert('No product subs list found');
                } else {

                    subsId = value.subsID;
                    substance = value.subs;
                    minPrice = value.MinPrice;
                    maxPrice = value.MaxPrice;
                    respPrice += '<li class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-last-child ui-btn-up-e">' + substance + minPrice + '-' + maxPrice + '</li>';
                    }

                    } 
 $('#productList').append(respPrice).listview('refresh');          
                });


                },
            error : function(e) {
                console.log(e.message);
                alert('Error Occoured');
            }
        });