0

这是创建数组并访问它们的代码。

<script>
    $(document).ready(
            function() {
                var i;
                var mycars = new Array();
                mycars[0] = "Car1";
                mycars[1] = "Car2";
                mycars[2] = "Car3";
                mycars[3] = "Car4";
    var Car1 = {
   ownerName: "Owner1",
   phoneNumber: "phoneNumber1",
   address: "Address1"
}
var Car2 = {
   ownerName: "Owner2",
   phoneNumber: "phoneNumber2",
   address: "Address2"
}
var Car3 = {
   ownerName: "Owner3",
   phoneNumber: "phoneNumber3",
   address: "Address3"
}
                $("div#tabbable").tabs();

                // $("button#add-tab").click(function() {

                var num_tabs = $("div#tabbable ul li").length + 1;
                for (i = 0; i < mycars.length; i++) {

                    $("div#tabbable ul").append(
                            "<li><a href='#tab" + num_tabs + "'>" + mycars[i]
                                    + "</a></li>");

                     $("div#tabbable").append(
                     "<div id='tab" + num_tabs + "'>#" + mycars[i] + "</div>"
                     );                     $("div#tabbable").tabs("refresh");
                }

                //});                    
            });
</script>
    <body>

<!-- needed for tabbable population -->
                <div class="row-fluid">
                    <div class="span5 offset1">
                        <div id='tabbable'>
                            <ul class="nav nav-tabs nav-stacked">

                                <li> 
                                    <!-- <a href='#tab1'>#1</a> --></li>
                            </ul>
                            <!-- <div id='tab1'></div> -->
                        </div>
                        <!-- <button id='add-tab'>Add tab</button> -->

                    </div>

                </div>
<!-- end -->

</body>

如果我需要每辆车的详细信息,例如如何制作一个依赖于 mycars 数组的数组,该怎么办。当我使用对象填充汽车详细信息时,如何在此代码中获取汽车的详细信息?单击填充的选项卡时如何获取这些汽车详细信息???

4

4 回答 4

0

您应该存储Objects而不是字符串:

var Car1 = {
   ownerName: "Owner1",
   phoneNumber: "phoneNumber1",
   address: "Address1"
}

通过指定字段名称访问它:

alert(myCars[0].ownerName);

或者

alert(myCars[0]["ownerName"]);

强烈推荐你阅读JavaScript 世界中的 OOP

于 2013-08-20T10:06:25.557 回答
0

您可以尝试以下方法:

var mycars = {};
mycars["car1"] = ["Owner1", "phone number1", "Address1"];
mycars["car2"] = ["Owner2", "phone number2", "Address2"];
mycars["car3"] = ["Owner3", "phone number3", "Address3"];
mycars["car4"] = ["Owner4", "phone number4", "Address4"];

for(var car in mycars) {
    // car = car1, car2, car 3, car4
    // mycars[car] = your array
}
于 2013-08-20T10:07:07.587 回答
0

我将创建一个Objects数组,如下所示:

function Car(settings) {
   this.Name = settings.Name;
   this.Owner = settings.Owner;
   this.Phone = settings.Phone;
   this.Address = settings.Address;
};
var mycars = [];
mycars.push(new Car({
   Name: "Car1",
   Owner: "Owner1",
   Phone: "Phone1",
   Address: "Address1"
}));

然后你可以像这样得到一个数组项的属性:

for (var i = 0; i < mycars.length; i++) {
   console.log(mycars[i].Name); 
   //or any other property, defined in Car constructor function
};
于 2013-08-20T10:08:14.967 回答
0

可以直接使用多维数组

 mycars = "{Car1:{ "Owner1", "phone number1", "Address1" }}";

我使用此代码创建多维数组并使用汽车名称(如 car1、car2 等)获取信息

于 2013-08-20T10:16:46.433 回答