2015-03-18 67 views
0

我不能得到的代碼返回對象和數組中保存要保存的數組中的對象,第一個功能是保存在陣列內的歌曲和第二功能它來搜索歌曲已保存在陣列中我的功能不會返回

function save() { 
    //loop to keep adding songs within the array 

    var addSong = //create the object inside the function 
    { 
     title: document.number.title.value, 
     artist: document.number.artist.value, 
     col: document.number.col.value, 
     dur: document.number.dur.value 
    }; 

    //output the song title of the saved song 
    document.number.saved.value = addSong.title; 
    return addSong; 
} 
//---------------------------------------------------------- 
var sTitle = document.searchSong.searchTitle.value; // assigning the search box to 'sTitle'  
var library = new Array() //array to store each song 
var song = addSong.title; 

//----------------------------------------------------------- 
//function to search for a song saved in the music() array 
function search() { 
    //if statement to compare the inputs and see if the song is in  the  
    library 
    if (sTitle == song) { 
     //if the song title is there then it is output 
     document.searchSong.result.value = song; 
    } else { 
     //if not then 'no song' is output instead 
     document.searchSong.result.value = "no song"; 
    } 
} 
+0

爲什麼所有這些屬性都添加到'document'中? – adeneo 2015-03-18 21:54:11

+0

因爲採用的形式讓用戶輸入IM到陣列 – 2015-03-18 21:55:50

+2

內保存這是整個腳本?因爲如果是這樣,你需要調用函數「保存()」,並試圖訪問它的「標題」屬性前轉讓其結果addSong。 – oLeduc 2015-03-18 21:56:50

回答

1

您需要調用函數save()並將其結果賦值給名爲addSong的變量。

var addSong = save(); 
var song = addSong.title; 
+0

三江源,我怎樣才能使搜索功能工作? i型在框中並且沒有出現與web控制檯未啓用未來與錯誤 – 2015-03-18 22:38:03

+0

我需要將數據存儲在陣列中,而不是一個可變的 – 2015-03-18 22:40:24