我无法获取返回对象并保存在数组中的代码,第一个功能是将歌曲保存在数组中,第二个功能是搜索已保存在数组中的歌曲

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";
    }
}

最佳答案

您需要调用函数save()并将其结果分配给名为addSong的变量。

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

关于javascript - 我的函数不会返回要保存在数组中的对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29133392/

10-12 12:46