将数组传递给javascript(for循环) [英] pass array to javascript(for loop)

查看:68
本文介绍了将数组传递给javascript(for循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码后面有3个数组:

I have 3 arrays at my code behind:

ArrayList ID = new Arraylist();
ID.Add("1");
ID.Add("2");
ID.Add("3");

ArrayList name = new Arraylist();
name.Add("A");
name.Add("B");
name.Add("C");

ArrayList age = new Arraylist();
age.Add("16");
age.Add("29");
age.Add("13");



我已经将数组传递给我的javascript,但是问题是它如何以这种格式显示:



I already pass the array to my javascript, but the problem is how can it display in this format:

ID: 1
name: A
age: 16

ID: 2
name: B
age: 29

ID:3
name: C
age: 13





how can this be done in a javascript for loop.

推荐答案

以下是一种非常简单的方法,假设使用VS模板中的asp.net项目. >
背后的代码:Default.aspx.cs
The following is a really minimalistic approach, assuming an asp.net project from the VS template.

Code behind: Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        public sealed class Person 
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }

        protected IList<person> persons = new List<person>();

        protected void Page_Load(object sender, EventArgs e)
        {
            persons.Add(new Person { ID = 1, Name = "Peter", Age = 20 });
            persons.Add(new Person { ID = 2, Name = "John", Age = 26 });
            persons.Add(new Person { ID = 2, Name = "Olivia", Age = 23 });
        }
    }
}


如您所见,我正在定义一个类,以及该类的列表,而不是数组.当然,不是三个数组,因为那是一个非常糟糕的方法.所以我有我的数据.
现在,在Defaul.aspx中,我嵌入了该对象的JSON编码表示.实际上,我将其传递给了javascript对象.


As you can see I am defining a class, and a list from that class, not an array. And of course, not three arrays, since that is a really bad approach. So I have my data.
Now, in the Defaul.aspx I embed the JSON encoded representation of that object. I actually pass it to a javascript object.

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"

    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="ScriptContent" runat="server" ContentPlaceHolderID="HeadContent">
 <script type="text/javascript">
  var persons = eval('(<% Response.Write(fastJSON.JSON.Instance.ToJSON(persons)); %>)');
 </script>
</asp:Content>

<asp:Content ID="MainContent" runat="server" ContentPlaceHolderID="MainContent">
 <script type="text/javascript">
     alert(persons);
 </script>
</asp:Content>


警报将显示一系列对象.现在轮到您处理变量了.我已经下载并引用了 FastJSON [


The alert will show an array of objects. Now it is your turn to process the variable. I have downloaded and referenced FastJSON[^] for this code to work. Notice, that you can add a second parameter to the serializer to fine tune the serialization. Look at the generated code to see how the serialization is performed.

Well, this is not the only approach, and could be not the most suitable for your needs. But you have not specified what you want to do.


这篇关于将数组传递给javascript(for循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆