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

Store users choice data in my array temporarily

本文关键字:存储 我的 阵列 用户 选择 数据      更新时间:2023-09-26

这是我的表单,下面是一个函数。我需要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)
}

首先,我建议将值存储在对象中,而不是数组中。当您有多个相关的数据片段时,使用它会容易得多。以下是它在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;
}

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