jquery 对象作为函数的参数

发布于 2024-12-09 11:22:40 字数 3384 浏览 2 评论 0原文

这是我的 jquery 响应:

[ 

    { "depot":
      { 
       "id": "D1",
        "intersection": {
            "first": "Markham",
            "second": "Lawrence"
        },
        "address": {
            "number": "25",
            "street": "Cougar Court",
            "city": "Scarborough",
            "province": "ON",
            "postal_code": "M1J"
        } 
     },
     "vehicle": [

            {
                "id": "V1",
                "depot_id": "D1",
                "model": "Ford Focus",
                "price": "45",
                "km_per_litre": "15",
                "cargo_cu_m": "YES",
                "category": "Compact car",
                "image": "www.coolcarz.com"
            }

                ,

            {
                "id": "V2",
                "depot_id": "D1",
                "model": "Honda Civic",
                "price": "45",
                "km_per_litre": "150",
                "cargo_cu_m": "YES",
                "category": "Compact car",
                "image": "www.coolcarz.com"
            }

                ,

            {
                "id": "V8",
                "depot_id": "D1",
                "model": "Pontiac Aztek",
                "price": "10",
                "km_per_litre": "6",
                "cargo_cu_m": "YES",
                "category": "SUV",
                "image": "www.nocoolcarz.com"
            }

                ,

            {
                "id": "V12",
                "depot_id": "D1",
                "model": "Chevrolet Impala",
                "price": "45",
                "km_per_litre": "12",
                "cargo_cu_m": "YES",
                "category": "Standard car",
                "image": "www.coolcarz.com"
            }

                ,

            {
                "id": "V29",
                "depot_id": "D1",
                "model": "Nissan Leaf",
                "price": "150",
                "km_per_litre": "0",
                "cargo_cu_m": "YES",
                "category": "Electronic Car",
                "image": "www.coolcarz.com"
            }

        ]
    }

     ,

    { "depot":
      { 
       "id": "A1",
        "intersection": {
            "first": "Markham",
            "second": "Lawrence"
        },
        "address": {
            "number": "25",
            "street": "Cougar Court",
            "city": "Scarborough",
            "province": "ON",
            "postal_code": "m1J"
        } 
     },
     "vehicle": [

        ]
    }

  ]

我想做的是,在我的代码中的某个时刻,当我收到此响应数据时,我想将 data[0] 或 data[0].vehicle[1] 传递给一个函数 我现在这样做的方式是:

function(data) {
    var items = [];
    for( i=0; i<data.length; i++){
        items.push('<b>' + data[i].depot.intersection.first+"-"+ data[i].depot.intersection.second + " depot has following cars:"+ '</b>');
        for( k=0; k<data[i].vehicle.length;k++){
            str = '<li> <a href="#" onClick="moreInfo('+data+')">' +       data[i].vehicle[k].category +"</a>, $"+ data[i].vehicle[k].price +' a day </li>';

                                    items.push(str);

实际上我正在尝试创建一个超链接(以车辆类别作为文本),当用户单击此超链接时,我想将车辆信息数组传递给一个名为 moreInfo 的新函数尽其所能。现在,当我执行此操作并单击超链接时,我看到错误

missing ] after element list
timepass([object Object],[object Object]

有任何想法吗?

This is my jquery response:

[ 

    { "depot":
      { 
       "id": "D1",
        "intersection": {
            "first": "Markham",
            "second": "Lawrence"
        },
        "address": {
            "number": "25",
            "street": "Cougar Court",
            "city": "Scarborough",
            "province": "ON",
            "postal_code": "M1J"
        } 
     },
     "vehicle": [

            {
                "id": "V1",
                "depot_id": "D1",
                "model": "Ford Focus",
                "price": "45",
                "km_per_litre": "15",
                "cargo_cu_m": "YES",
                "category": "Compact car",
                "image": "www.coolcarz.com"
            }

                ,

            {
                "id": "V2",
                "depot_id": "D1",
                "model": "Honda Civic",
                "price": "45",
                "km_per_litre": "150",
                "cargo_cu_m": "YES",
                "category": "Compact car",
                "image": "www.coolcarz.com"
            }

                ,

            {
                "id": "V8",
                "depot_id": "D1",
                "model": "Pontiac Aztek",
                "price": "10",
                "km_per_litre": "6",
                "cargo_cu_m": "YES",
                "category": "SUV",
                "image": "www.nocoolcarz.com"
            }

                ,

            {
                "id": "V12",
                "depot_id": "D1",
                "model": "Chevrolet Impala",
                "price": "45",
                "km_per_litre": "12",
                "cargo_cu_m": "YES",
                "category": "Standard car",
                "image": "www.coolcarz.com"
            }

                ,

            {
                "id": "V29",
                "depot_id": "D1",
                "model": "Nissan Leaf",
                "price": "150",
                "km_per_litre": "0",
                "cargo_cu_m": "YES",
                "category": "Electronic Car",
                "image": "www.coolcarz.com"
            }

        ]
    }

     ,

    { "depot":
      { 
       "id": "A1",
        "intersection": {
            "first": "Markham",
            "second": "Lawrence"
        },
        "address": {
            "number": "25",
            "street": "Cougar Court",
            "city": "Scarborough",
            "province": "ON",
            "postal_code": "m1J"
        } 
     },
     "vehicle": [

        ]
    }

  ]

What I want to do is that at some point in my code, when I have received this response data, I want to pass , say data[0] or or data[0].vehicle[1] to a function
The way I am doing it now is:

function(data) {
    var items = [];
    for( i=0; i<data.length; i++){
        items.push('<b>' + data[i].depot.intersection.first+"-"+ data[i].depot.intersection.second + " depot has following cars:"+ '</b>');
        for( k=0; k<data[i].vehicle.length;k++){
            str = '<li> <a href="#" onClick="moreInfo('+data+')">' +       data[i].vehicle[k].category +"</a>, $"+ data[i].vehicle[k].price +' a day </li>';

                                    items.push(str);

in effect I am trying to create a hyperlink (with vehicle category as text), and when user clicks on this hyperlink, I want to pass on the vehicle information array to a new function called moreInfo that does its job. Rit now when I do this and click the hyperlink, I see the error

missing ] after element list
timepass([object Object],[object Object]

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悟红尘 2024-12-16 11:22:40

从 json 代码的开头和结尾删除 2 个方括号,并将其用作对象而不是数组。

您所做的是创建一个对象数组而不是 JSON 对象

Remove 2 square brackets from beginning and end of your json code and use use it as an object not as an array.

What you did was creating an array of objects not JSON object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文