暂时将用户选择数据存储在我的阵列中

时间:2015-03-24 23:36:19

标签: javascript arrays forms function

这是我的表格,下面是一个功能。我需要3个函数,1个用于存储数据,1个用于保存数据,1个用于显示所有存储的数据。我怎样才能完成所有这些功能,使它们彼此对应。

注意:用户将输入数据,以便临时保存用户选择数据。用户应该能够单击按钮来使用该功能,但我该如何正确地完成这三项功能。

<form name = "Music">

            <p>Input a Song Name, Artist, Collaborations, Duration or Album</p>

            <input type="text" name = "Song_Name" id= "Song_Name" size="20"> <br> 
            <input type="text" name = "Artist" id= "Artist" size="20"> <br> 
            <input type="text" name = "Collaborations" id= "Collaborations" size="20"> <br> 
            <input type="text" name = "Duration" id= "Duration" size="20"> <br> 
            <input type="text" name = "Album" id= "Album" size="20"> <br> 

            <input type="button" value = "Save" onclick = "Save()"> <br>
            <input type="button" value = "Search" onclick = "Store()"> <br>
            <input type="button" value = "Show_All" onclick = "Show_All()"> <br>

保存功能:

var catalogue[];

function Save = Music.Song_Name.Artist.Collaborations.Duration.Album.Save{

            var SongName = document.getElementById('Song_Name').value;
            var Artist = document.getElementById('Artist').value;
            var Collaborations = document.getElementById('Collaborations').value;
            var Duration = document.getElementById('Duration').value;
            var Album = document.getElementById('Album').value;

        document.write("You have chosen "+ Song_Name + Artist + Collaborations + Duration + Album)
}

1 个答案:

答案 0 :(得分:0)

首先,我建议将值存储在对象而不是数组中。当您拥有多个相关的数据时,使用它会容易得多。以下是Store and Show的工作原理:

var catalogue;

function Store() {
    catalogue = {
        SongName: document.getElementById('Song_Name').value;
        Artist: document.getElementById('Artist').value;
        Collaborations: document.getElementById('Collaborations').value;
        Duration: document.getElementById('Duration').value;
        Album: document.getElementById('Album').value;
    }
}

function Show_All() {
    document.getElementById('Song_Name').value = catalogue.SongName;
    document.getElementById('Artist').value = catalogue.Artist;
    document.getElementById('Collaborations').value = catalogue.Collaborations;
    document.getElementById('Duration').value = catalogue.Duration;
    document.getElementById('Album').value = catalogue.Album;
}

如何处理“保存”将取决于您要将其保存到的位置,但一般的想法是您可以将目录值直接传递到保存它的任何位置,或者解析属性目录对象,并将每个目标对象传递到您保存它们的任何位置。