英文:
how to show search result in another page using javascript
问题
我想要在另一个页面中显示表单的结果。我尝试使用 window.href.location
和 localStorage
重定向结果,但它没有重定向到另一个页面。我使用 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(".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("");
}
// check when the form is submitted
moviesForm.addEventListener("submit", (event) => {
const searchBarValue = document.getElementById("searchbar").value; // get input's value everytime the form is submitted
console.log(searchBarValue);
event.preventDefault(); // prevent refresh
// localStorage.setItem("key", searchBarValue);
// const searchKey = localStorage.getItem("key");
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
// }
<!-- language: lang-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>
<!-- 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:
<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 for this:
<script>
const moviesForm = document.getElementById("movie__searchbox");
// check when the form is submitted
moviesForm.addEventListener("submit", (event) => {
const searchBarValue = document.getElementById("searchbar").value;
event.preventDefault();
window.location.href = `movieList.html?value=${searchBarValue}`;
});
</script>
The Movie List Page:
<div class="shows"></div>
And The Script for this page (Api fetch for movie as per urlParams):
<script>
// Retrieve the data from the URL parameters or local storage
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>
答案2
得分: 0
你可以使用window.href.location
和localStorage
本身来实现你正在做的事情。
我创建了两个不同的文件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
英文:
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 -->
<!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; // get input's value everytime the form is submitted
console.log(searchBarValue);
event.preventDefault(); // prevent refresh
// localStorage.setItem("key", searchBarValue);
// const searchKey = localStorage.getItem("key");
main(searchBarValue);
});
</script>
</body>
</html>
<!-- end snippet -->
movies.html
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-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>
<!-- end snippet -->
Results
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论