Searchbox Javascript-以与数据库中的值匹配的形式显示值警报

Searchbox Javascript-以与数据库中的值匹配的形式显示值警报,javascript,forms,for-loop,Javascript,Forms,For Loop,抱歉,我以前来过这里,但我似乎无法在表单中输入任何搜索值以显示在警报中。我认为这与for循环有关。我觉得其他一切都很好。有什么想法吗 TGIF 首先,上面的代码有几个错误 附加'};'搜索功能 init函数在方法定义中没有括号 在搜索函数中,您使用id“searchBox”作为文本框的id,在init中,您使用它作为表单的id 我看不出使用sessionStorage的理由 下面是固定代码 JavaScript部分 HTML部分 var fakeDatabase = []; var foxyS

抱歉,我以前来过这里,但我似乎无法在表单中输入任何搜索值以显示在警报中。我认为这与for循环有关。我觉得其他一切都很好。有什么想法吗

TGIF


首先,上面的代码有几个错误

附加'};'搜索功能 init函数在方法定义中没有括号 在搜索函数中,您使用id“searchBox”作为文本框的id,在init中,您使用它作为表单的id 我看不出使用sessionStorage的理由 下面是固定代码

JavaScript部分

HTML部分

var fakeDatabase = [];

var foxyShazam = {
    id: 'foxyShazam_FS',
    title: 'Foxy Shazam',
    artist: 'Foxy Shazam',
    price: '$14.99',
    releaseDate: new Date(1968, 10, 22),
    quantity: 50,
    link: "albums/foxyShazam.html",
    Trackinglist: ["Intro: Bombs Away", "Wanna-Be Angel", "Count Me Out", "Bye Bye Symphony", "Unstoppable", "Second Floor", "Oh Lord", "Connect",  "The Only Way to My Heart...", "Killin' It", "Evil Thoughts"]
};

var foxyShazam2 = {
    id: 'foxyShazam_FS_2',
    title: 'Foxy Shazam 2',
    artist: 'Foxy Shazam 2',
    price: '$14.99',
    releaseDate: new Date(1968, 10, 22),
    quantity: 50,
    link: "albums/foxyShazam.html",
    Trackinglist: ["Intro: Bombs Away", "Wanna-Be Angel", "Count Me Out", "Bye Bye Symphony", "Unstoppable", "Second Floor", "Oh Lord", "Connect",  "The Only Way to My Heart...", "Killin' It", "Evil Thoughts"]
};

var thriller = {
    id: 'thriller_MJ',
    title: 'Thriller',
    artist: 'Michael Jackson',
    price: '$12.99',
    releaseDate: new Date(1982, 10, 30),
    quantity: 35,
    link: "albums/thriller.html",
    Trackinglist: ["Wanna Be Startin Somethin", "Baby Be Mine", "The Girl Is Mine", "Thriller", "Beat It", "Billie Jean", "Human Nature", "P.Y.T. (Pretty Young Thing)", "The Lady in My Life"]
};

var thriller2 = {
    id: 'thriller_MJ_2',
    title: 'Thriller 2',
    artist: 'Michael Jackson 2',
    price: '$12.99',
    releaseDate: new Date(1982, 10, 30),
    quantity: 35,
    link: "albums/thriller.html",
    Trackinglist: ["Wanna Be Startin Somethin", "Baby Be Mine", "The Girl Is Mine", "Thriller", "Beat It", "Billie Jean", "Human Nature", "P.Y.T. (Pretty Young Thing)", "The Lady in My Life"]
};

var millennium = {
    id: 'millennium_BSB',
    title: 'Millennium',
    artist: 'Backstreet Boys',
    price: '$7.99',
    releaseDate: new Date(1999, 4, 18),
    quantity: 15,
    link: "albums/millennium.html",
    Trackinglist: ["Larger Than Life", "I Want It That Way", "Show Me the Meaning", "It's Gotta Be You", "I Need You Tonight", "Don't Want You Back", "Don't Wanna Lose You Now", "The One", "Back to Your Heart", "Spanish Eyes", "No One Else Comes Close", "The Perfect Fan"]
};

var darkSideOfTheMoon = {
    id: 'darkSideOfTheMoon_PinkFloyd',
    title: 'Dark Side of the Moon',
    artist: 'Pink Floyd',
    price: '$14.99',
    releaseDate: new Date(1973, 2, 1),
    quantity: 60,
    link: "albums/darkSideOfTheMoon.html",
    Trackinglist: ["Speak to Me", "Breathe", "On the Run", "Time", "The Great Gig in the Sky", "Money", "Us and Them", "Any Colour You Like", "Brian Damage", "Eclipse"]
};

fakeDatabase.push(foxyShazam, foxyShazam2, thriller, thriller2, millennium, darkSideOfTheMoon);

function search() {
    'use strict';
    var searchVal = document.getElementById('searchBox').value;
    sessionStorage.setItem('searchVal', searchVal);
    sessionStorage.getItem('searchVal');
    for (var i=0; i < fakeDatabase.length; i++) {
        if (searchVal.value == (fakeDatabase[i].artist) {
        alert(fakeDatabase[i].artist);
        };
    };
    };
};

function init {
    "use strict";
    document.getElementById('searchBox').onsubmit = search;
    searchBox.onsubmit = search(fakeDatabase);
        };

window.onload = init();
 var fakeDatabase = [];

    var foxyShazam = {
        id: 'foxyShazam_FS',
        title: 'Foxy Shazam',
        artist: 'Foxy Shazam',
        price: '$14.99',
        releaseDate: new Date(1968, 10, 22),
        quantity: 50,
        link: "albums/foxyShazam.html",
        Trackinglist: ["Intro: Bombs Away", "Wanna-Be Angel", "Count Me Out", "Bye Bye Symphony", "Unstoppable", "Second Floor", "Oh Lord", "Connect",  "The Only Way to My Heart...", "Killin' It", "Evil Thoughts"]
    };

    var foxyShazam2 = {
        id: 'foxyShazam_FS_2',
        title: 'Foxy Shazam 2',
        artist: 'Foxy Shazam 2',
        price: '$14.99',
        releaseDate: new Date(1968, 10, 22),
        quantity: 50,
        link: "albums/foxyShazam.html",
        Trackinglist: ["Intro: Bombs Away", "Wanna-Be Angel", "Count Me Out", "Bye Bye Symphony", "Unstoppable", "Second Floor", "Oh Lord", "Connect",  "The Only Way to My Heart...", "Killin' It", "Evil Thoughts"]
    };

    var thriller = {
        id: 'thriller_MJ',
        title: 'Thriller',
        artist: 'Michael Jackson',
        price: '$12.99',
        releaseDate: new Date(1982, 10, 30),
        quantity: 35,
        link: "albums/thriller.html",
        Trackinglist: ["Wanna Be Startin Somethin", "Baby Be Mine", "The Girl Is Mine", "Thriller", "Beat It", "Billie Jean", "Human Nature", "P.Y.T. (Pretty Young Thing)", "The Lady in My Life"]
    };

    var thriller2 = {
        id: 'thriller_MJ_2',
        title: 'Thriller 2',
        artist: 'Michael Jackson 2',
        price: '$12.99',
        releaseDate: new Date(1982, 10, 30),
        quantity: 35,
        link: "albums/thriller.html",
        Trackinglist: ["Wanna Be Startin Somethin", "Baby Be Mine", "The Girl Is Mine", "Thriller", "Beat It", "Billie Jean", "Human Nature", "P.Y.T. (Pretty Young Thing)", "The Lady in My Life"]
    };

    var millennium = {
        id: 'millennium_BSB',
        title: 'Millennium',
        artist: 'Backstreet Boys',
        price: '$7.99',
        releaseDate: new Date(1999, 4, 18),
        quantity: 15,
        link: "albums/millennium.html",
        Trackinglist: ["Larger Than Life", "I Want It That Way", "Show Me the Meaning", "It's Gotta Be You", "I Need You Tonight", "Don't Want You Back", "Don't Wanna Lose You Now", "The One", "Back to Your Heart", "Spanish Eyes", "No One Else Comes Close", "The Perfect Fan"]
    };

    var darkSideOfTheMoon = {
        id: 'darkSideOfTheMoon_PinkFloyd',
        title: 'Dark Side of the Moon',
        artist: 'Pink Floyd',
        price: '$14.99',
        releaseDate: new Date(1973, 2, 1),
        quantity: 60,
        link: "albums/darkSideOfTheMoon.html",
        Trackinglist: ["Speak to Me", "Breathe", "On the Run", "Time", "The Great Gig in the Sky", "Money", "Us and Them", "Any Colour You Like", "Brian Damage", "Eclipse"]
    };

    fakeDatabase.push(foxyShazam, foxyShazam2, thriller, thriller2, millennium, darkSideOfTheMoon);

    function search() {
        'use strict';
        var searchVal = document.getElementById('searchBox').value;
        sessionStorage.setItem('searchVal', searchVal);
        sessionStorage.getItem('searchVal');
        for (var i=0; i < fakeDatabase.length; i++) {
            if (searchVal === fakeDatabase[i].artist) {
                alert(fakeDatabase[i].artist);
            };
        };
    };
<form onsubmit="search()">
    <input type="text" id="searchBox">
    <input type="submit" value="submit">
</form>