如何使用JavaScript在另一页显示搜索结果。

huangapple go评论60阅读模式
英文:

how to show search result in another page using javascript

问题

我想要在另一个页面中显示表单的结果。我尝试使用 window.href.locationlocalStorage 重定向结果,但它没有重定向到另一个页面。我使用 click() 来点击 button,看看它是否会在另一个页面上显示,但我不确定我做错了什么。搜索栏位于 index.html 中,当我点击 search 按钮后,我希望它在另一个 html 文件中显示结果,例如 movies.html

<form action="#" id="movie__searchbox" class="nav__search-bar">
    <input type="text" id="searchbar" class="nav__search" placeholder="Search" />
    <button onclick="click()" type="submit">Search</button>
</form>
<div class="shows"></div>
const movieListEl = document.querySelector(".shows");
const moviesForm = document.getElementById("movie__searchbox");

async function main(event) {
    const url = `https://www.omdbapi.com/?apikey=39a36280&s=${encodeURI(event)}`;
    const search = await fetch(`${url}`);
    const data = await search.json();
    movieListEl.innerHTML = data.Search.map((movie) => movieHTML(movie)).join("");
}

moviesForm.addEventListener("submit", (event) => {
    const searchBarValue = document.getElementById("searchbar").value;
    console.log(searchBarValue);
    event.preventDefault();
    main(searchBarValue);
});

function movieHTML(details) {
    return `<div class="shows__content">
        <figure class="shows__poster">
            <img src="${details.Poster}" alt="" class="poster">
        </figure>
        <div class="poster__description">
            <h4 class="poster__title">${details.Title}</h4>
            <p class="poster__year">${details.Year}</p>
        </div>
    </div>`;
}

function click() {
    console.log(window.location);
    window.location.href = `${window.location.origin}/movies/movies.html`;
}

请注意,你的 click() 函数在 HTML 中有一个调用,当点击按钮时,它将重定向到指定的页面。

英文:

I wanted to show the result of the form into another page. I tried redirecting the result using the window.href.location and localStorage but it's not redirecting it to another page. I used the click() for the button to see if it will show on the other page but I'm not sure what I'm doing wrong. The search bar is in index.html and after I click the search button, I wanted it show the result in another html file like movies.html

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

 const movieListEl = document.querySelector(&quot;.shows&quot;);
const moviesForm = document.getElementById(&quot;movie__searchbox&quot;);

async function main(event) {
    const url = `https://www.omdbapi.com/?apikey=39a36280&amp;s=${encodeURI(event)}`;
    const search = await fetch(`${url}`);
    const data = await search.json();
    movieListEl.innerHTML = data.Search.map((movie) =&gt; movieHTML(movie)).join(&quot;&quot;);
}

// check when the form is submitted
moviesForm.addEventListener(&quot;submit&quot;, (event) =&gt; {
    const searchBarValue = document.getElementById(&quot;searchbar&quot;).value; // get input&#39;s value everytime the form is submitted
    console.log(searchBarValue);
    event.preventDefault(); // prevent refresh
    // localStorage.setItem(&quot;key&quot;, searchBarValue);
    // const searchKey = localStorage.getItem(&quot;key&quot;);
    main(searchBarValue);
});

function movieHTML(details) {
    return `&lt;div class=&quot;shows__content&quot;&gt;
        &lt;figure class=&quot;shows__poster&quot;&gt;
            &lt;img src=&quot;${details.Poster}&quot; alt=&quot;&quot; class=&quot;poster&quot;&gt;
        &lt;/figure&gt;
           &lt;div class=&quot;poster__description&quot;&gt;
            &lt;h4 class=&quot;poster__title&quot;&gt;${details.Title}&lt;/h4&gt;
            &lt;p class=&quot;poster__year&quot;&gt;${details.Year}&lt;/p&gt;
        &lt;/div&gt;
    &lt;/div&gt;
        `;
}

//function click () {
// console.log(window.location)
// window.location.href = ${window.location.origin}/movies/movies.html
// }

<!-- language: lang-html -->

  &lt;form action=&quot;#&quot; id=&quot;movie__searchbox&quot; class=&quot;nav__search-bar&quot;&gt;
    &lt;input type=&quot;text&quot; id=&quot;searchbar&quot; class=&quot;nav__search&quot; placeholder=&quot;Search&quot; /&gt;
    &lt;button onclick=&quot;click()&quot; type=&quot;submit&quot;&gt;Search&lt;/button&gt;
&lt;/form&gt;
&lt;div class=&quot;shows&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

在您的主页上,不要获取您的 API 数据,只需将其重定向到要显示电影列表结果的页面,只传递一个参数,即您的搜索值。

在您想要显示电影列表的其他页面上,获取 urlParams,然后获取您的 API 数据并按照我提供的以下代码显示您的电影列表。

主页代码如下

<form action="#" id="movie__searchbox" class="nav__search-bar">
  <input type="text" id="searchbar" class="nav__search" placeholder="Search" />
  <button type="submit">Search</button>
</form>

此页面的脚本如下

<script>
const moviesForm = document.getElementById("movie__searchbox");
// 当表单提交时检查
moviesForm.addEventListener("submit", (event) => {
  const searchBarValue = document.getElementById("searchbar").value;
  event.preventDefault();
  window.location.href = `movieList.html?value=${searchBarValue}`;
});
</script>

电影列表页面如下

<div class="shows"></div>

以及此页面的脚本(根据 urlParams 获取电影的 API 数据)

<script>
// 从 URL 参数或本地存储中检索数据
const urlParams = new URLSearchParams(window.location.search);
const value = urlParams.get('value');
const movieListEl = document.querySelector(".shows");

async function main(event) {
    const url = `https://www.omdbapi.com/?apikey=39a36280&s=${encodeURI(event)}`;
    const search = await fetch(`${url}`);
    const data = await search.json();
    movieListEl.innerHTML = data.Search.map((movie) => movieHTML(movie)).join("");
}
main(value);

function movieHTML(details) {
    return `<div class="shows__content">
        <figure class="shows__poster">
            <img src="${details.Poster}" alt="" class="poster">
        </figure>
        <div class="poster__description">
            <h4 class="poster__title">${details.Title}</h4>
            <p class="poster__year">${details.Year}</p>
        </div>
    </div>`;
}
</script>

请注意,我只提供了翻译,没有回答任何问题。如果您需要进一步的帮助,请随时提问。

英文:

On your index page don't fetch your api just redirect it to that page where you want to show the result of movie list, just pass single parameter that is your search value.

In your other page where you want to display movie list get the urlParams and then fetch your api and display your movie list as per the given code which I provided below.

The index page code is:

&lt;form action=&quot;#&quot; id=&quot;movie__searchbox&quot; class=&quot;nav__search-bar&quot;&gt;
&lt;input type=&quot;text&quot; id=&quot;searchbar&quot; class=&quot;nav__search&quot; placeholder=&quot;Search&quot; /&gt;
&lt;button type=&quot;submit&quot;&gt;Search&lt;/button&gt;

</form>

Script for this:

 &lt;script&gt;
const moviesForm = document.getElementById(&quot;movie__searchbox&quot;);
// check when the form is submitted
moviesForm.addEventListener(&quot;submit&quot;, (event) =&gt; {
  const searchBarValue = document.getElementById(&quot;searchbar&quot;).value;
  event.preventDefault();
  window.location.href = `movieList.html?value=${searchBarValue}`;
});

</script>

The Movie List Page:

&lt;div class=&quot;shows&quot;&gt;&lt;/div&gt;

And The Script for this page (Api fetch for movie as per urlParams):

&lt;script&gt;
    // Retrieve the data from the URL parameters or local storage
    const urlParams = new URLSearchParams(window.location.search);
    const value = urlParams.get(&#39;value&#39;);
    const movieListEl = document.querySelector(&quot;.shows&quot;);
    async function main(event) {
        const url = `https://www.omdbapi.com/?apikey=39a36280&amp;s=${encodeURI(event)}`;
        const search = await fetch(`${url}`);
        const data = await search.json();
        movieListEl.innerHTML = data.Search.map((movie) =&gt; movieHTML(movie)).join(&quot;&quot;);
    }
    main(value);
    function movieHTML(details) {
        return `&lt;div class=&quot;shows__content&quot;&gt;
    &lt;figure class=&quot;shows__poster&quot;&gt;
        &lt;img src=&quot;${details.Poster}&quot; alt=&quot;&quot; class=&quot;poster&quot;&gt;
    &lt;/figure&gt;
       &lt;div class=&quot;poster__description&quot;&gt;
        &lt;h4 class=&quot;poster__title&quot;&gt;${details.Title}&lt;/h4&gt;
        &lt;p class=&quot;poster__year&quot;&gt;${details.Year}&lt;/p&gt;
    &lt;/div&gt;
&lt;/div&gt;`;}
&lt;/script&gt;

答案2

得分: 0

你可以使用window.href.locationlocalStorage本身来实现你正在做的事情。

我创建了两个不同的文件index.html和movies.html。

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="#" id="movie__searchbox" class="nav__search-bar">
        <input type="text" id="searchbar" class="nav__search" placeholder="Search" />
        <button onclick="click()" type="submit">Search</button>
    </form>
    <script>
        
        const moviesForm = document.getElementById("movie__searchbox");

        async function main(event) {
            const url = `https://www.omdbapi.com/?apikey=39a36280&s=${encodeURI(event)}`;
            const search = await fetch(`${url}`);
            const data = await search.json();
            localStorage.setItem("movie_list", JSON.stringify(data.Search));
            window.location.href = `${window.location.origin}/movies.html`
        }

        // check when the form is submitted
        moviesForm.addEventListener("submit", (event) => {
            const searchBarValue = document.getElementById("searchbar").value; 
            console.log(searchBarValue);
            event.preventDefault();
            main(searchBarValue);
        });
    </script>
</body>
</html>

movies.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="shows"></div>
    <script>
        const movie_list = JSON.parse(localStorage.getItem("movie_list"))
        const movieListEl = document.querySelector(".shows");
        movieListEl.innerHTML = movie_list.map((movie) => movieHTML(movie)).join("");

        function movieHTML(details) {
            return `<div class="shows__content">
            <figure class="shows__poster">
            <img src="${details.Poster}" alt="" class="poster">
            </figure>
            <div class="poster__description">
            <h4 class="poster__title">${details.Title}</h4>
            <p class="poster__year">${details.Year}</p>
            </div>
            </div>
            `;
        }
    </script>
</body>
</html>

Results

如何使用JavaScript在另一页显示搜索结果。

如何使用JavaScript在另一页显示搜索结果。

英文:

You can achieve what you are doing with window.href.location and localStorage itself.

I created 2 different files index.html, movies.html

index.html

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form action=&quot;#&quot; id=&quot;movie__searchbox&quot; class=&quot;nav__search-bar&quot;&gt;
        &lt;input type=&quot;text&quot; id=&quot;searchbar&quot; class=&quot;nav__search&quot; placeholder=&quot;Search&quot; /&gt;
        &lt;button onclick=&quot;click()&quot; type=&quot;submit&quot;&gt;Search&lt;/button&gt;
    &lt;/form&gt;
    &lt;script&gt;
        
        const moviesForm = document.getElementById(&quot;movie__searchbox&quot;);

        async function main(event) {
            const url = `https://www.omdbapi.com/?apikey=39a36280&amp;s=${encodeURI(event)}`;
            const search = await fetch(`${url}`);
            const data = await search.json();
            localStorage.setItem(&quot;movie_list&quot;, JSON.stringify(data.Search));
            window.location.href = `${window.location.origin}/movies.html`
        }

        // check when the form is submitted
        moviesForm.addEventListener(&quot;submit&quot;, (event) =&gt; {
            const searchBarValue = document.getElementById(&quot;searchbar&quot;).value; // get input&#39;s value everytime the form is submitted
            console.log(searchBarValue);
            event.preventDefault(); // prevent refresh
            // localStorage.setItem(&quot;key&quot;, searchBarValue);
            // const searchKey = localStorage.getItem(&quot;key&quot;);
            main(searchBarValue);
        });
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

movies.html

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div class=&quot;shows&quot;&gt;&lt;/div&gt;
    &lt;script&gt;
        const movie_list = JSON.parse(localStorage.getItem(&quot;movie_list&quot;))
        const movieListEl = document.querySelector(&quot;.shows&quot;);
        movieListEl.innerHTML = movie_list.map((movie) =&gt; movieHTML(movie)).join(&quot;&quot;);

        function movieHTML(details) {
            return `&lt;div class=&quot;shows__content&quot;&gt;
            &lt;figure class=&quot;shows__poster&quot;&gt;
            &lt;img src=&quot;${details.Poster}&quot; alt=&quot;&quot; class=&quot;poster&quot;&gt;
            &lt;/figure&gt;
            &lt;div class=&quot;poster__description&quot;&gt;
            &lt;h4 class=&quot;poster__title&quot;&gt;${details.Title}&lt;/h4&gt;
            &lt;p class=&quot;poster__year&quot;&gt;${details.Year}&lt;/p&gt;
            &lt;/div&gt;
            &lt;/div&gt;
            `;
        }
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

Results

如何使用JavaScript在另一页显示搜索结果。

如何使用JavaScript在另一页显示搜索结果。

huangapple
  • 本文由 发表于 2023年7月28日 00:26:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定