0

这是一个学校项目!我只有几周的时间来学习 JavaScript - 我正在尽我最大的努力学习它,但我真的需要帮助,所以我再次向那些精通和知识渊博的人寻求帮助......

没有代码错误出现,并且不确定为什么这不会显示在该项目的“管理”页面上的 ALERT 窗口中。任何想法或想法将不胜感激!谢谢...

这是 JSFiddle - http://jsfiddle.net/qux3K/1/

这是代码:

// Chapters 7 & 8 - store.js

// 此脚本创建一个“假”数据库,将可用的 CD 存储在我们的音乐商店收藏中。

// Function called when page is loaded.
// Function creates a new object.
function process() {
'use strict';


// Create a new array:
var cd = new Array ([
    {"cd":0, "item":"001", "title": "Total Life Forever", "artist": "FOALS",         "price": "$14.99", "released": "March, 2009", "tracks": "Track Listing"     {1,2,3,4,5,6,7,8,9,10,11,12}, "quantity": "1"}
    {"cd":1, "item":"002", "title": "Bein Love", "artist": "Locksley", "price": "$14.99", "released": "April, 2012", "tracks": "Track Listing" {1,2,3,4,5,6,7,8,9,10,11,12}, "quantity": "1"}
    {"cd":2, "item":"003", "title": "Privileged", "artist": "Nick Moss", "price": "$14.99", "released": "June, 2011", "tracks": "Track Listing" {1,2,3,4,5,6,7,8,9,10,11,12}, "quantity": "1"}
    {"cd":3, "item":"004", "title": "Asondeguerra", "artist": "Juan Louis Guerra", "price": "$14.99", "released": "September, 2013", "tracks": "Track Listing" {1,2,3,4,5,6,7,8,9,10,11,12}, "quantity": "1"}
    {"cd":4, "item":"017", "title": "Way Out Here", "artist": "Josh Thompson", "price": "$14.99", "released": "August, 2010", "tracks": "Track Listing" {1,2,3,4,5,6,7,8,9,10,11,12}, "quantity": "1"}
    {"cd":5, "item":"018", "title": "Tremolo", "artist": "The Pines", "price": "$14.99", "released": "January, 2007", "tracks": "Track Listing" {1,2,3,4,5,6,7,8,9,10,11,12}, "quantity": "1"}
    {"cd":6, "item":"019", "title": "Live From Freedom Hall", "artist": "Lynyrd Skynyrd", "price": "$14.99", "released": "June, 2010", "tracks": "Track Listing" {1,2,3,4,5,6,7,8,9,10,11,12}, "quantity": "1"}
    {"cd":7, "item":"020", "title": "Achin' & Shakin'", "artist": "Laura Bell Bundy", "price": "$14.99", "released": "July, 2013", "tracks": "Track Listing" {1,2,3,4,5,6,7,8,9,10,11,12}, "quantity": "1"}
    {"cd":8, "item":"021", "title": "Here I Am", "artist": "Marvin Sapp", "price": "$14.99", "released": "November, 2011", "tracks": "Track Listing" {1,2,3,4,5,6,7,8,9,10,11,12}, "quantity": "1"}
    {"cd":9, "item":"022", "title": "Just James", "artist": "J Moss", "price": "$14.99", "released": "March, 2011", "tracks": "Track Listing" {1,2,3,4,5,6,7,8,9,10,11,12}, "quantity": "1"}
    {"cd":10, "item":"013", "title": "Tom Petty - Live", "artist": "Tom Petty", "price": "$14.99", "released": "May, 2010", "tracks": "Track Listing" {1,2,3,4,5,6,7,8,9,10,11,12}, "quantity": "1"}
]); 


// Function to call titles in an alert box
alert('CD titles available are' + cd({title:"0,1,2,3,4,5,6,7,8,9,10"}));
// End of alert.    


} 
// End of process() function.

// Initial setup:
function init() {
'use strict';
document.getElementById('theForm').onsubmit = process;

} // End of init() function.
window.onload = init;
4

2 回答 2

0

代替

var cd = new Array ([
    {"cd":0, "item":"001", "title": "Total Life Forever", "artist": "FOALS",             "price": "$14.99", "released": "March, 2009", "tracks": "Track Listing"     {1,2,3,4,5,6,7,8,9,10,11,12}, "quantity": "1"},
    // ...
]);

写吧

var cd = [
    {...},
    {...},
];

并且cd({...})不起作用,因为 cd 是一个数组而不是一个函数。

您将(必须)在学校学到很多东西;-)

于 2013-11-12T07:08:46.073 回答
0

你有几个错误。

如果您使用 Array 构造函数作为

var cd = new Array ([...]);

您实际上定义了对象数组的数组。你只需要使用

var cd = new Array(...)

要不就

var cd = [...];

每个数组项,在你的情况下是对象,在它之后需要','

var cd = new Array(1, 2, 3)

在你的情况下:

var cd = new Array({...}, {...}, {...});

有一个对象属性

"tracks": "Track Listing"     {1,2,3,4,5,6,7,8,9,10,11,12},

在这里,您将“:”字符串和对象 {1, 2...} 放在后面。你想定义什么?

"tracks": {1,2,3,4,5,6,7,8,9,10,11,12},

或者是其他东西?

例如,您可以使用 for 循环获取每个标题

for (var i = 0; i < cd.length; i++)
    alert("CD Title: " + cd[i].title);
于 2013-11-12T07:28:21.860 回答