English 中文(简体)
json 阵列中的阵列
原标题:Array inside json array

我得到的结果像

{"ID":1022,"Type":"type1","Name":"name1","Values":[{"ID":3540,"Name":"1"},{"ID":3541,"Name":"2"},{"ID":3542,"Name":"cb"}]}

成功后,我得到这样的功能

   success: function(data) {
            $.each(data, function() {
                $( #properties ).append(
                this.ID + "," +
                this.Name + "," +

                this.type + "," +
                    this.values
                );
            });
        }

但“值”是另一个阵列, 如何显示?

最佳回答
//get the total length of values array.
this.Values.length;

for (  var i = 0 ; i < this.Values.length; i++ ){
 var resultId= this.Values[i].ID;
 var resultName = this.Values[i].Name;
}

这工作。

问题回答

我相信 this.Values[0].ID 应该正确返回第一个元素的 ID 。

您也可以以同样的方式浏览阵列中的每个项目并访问它。

for (var i = 0; i < this.Values.length; i++) {
    alert(this.Values[i].ID); //Show an alert for each ID.
}

this.values[0].ID may works, but, Im not 完全肯定。 不过,我猜尝试没有坏处。

我不太清楚这在最内部函数中的价值是什么, 但是它可能不是你想要的。 为了将变量传递给创建时的函数, 而不是被调用时, 您应该用另一个函数来包装它 :

{
    success: function(data) {
        $.each(data, (function(dat) { return function(index, value) {
            $( #properties ).append(
               value.ID + "," +
               value.Name + "," +
               dat.type + "," +
               dat.values
            );
        }; ) (data));
    }
};




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签