gpt4 book ai didi

javascript - 每次用户单击输入中的空格键时如何添加 + 键

转载 作者:行者123 更新时间:2023-12-03 00:37:05 27 4
gpt4 key购买 nike

我使用 OMDB API 创建了一个电影搜索页面。我遇到的问题是,如果我搜索一部包含多个单词的电影,此 API 会出错,因为 API 的 URL 必须在 URL 中的每个单词之间有 + 键。所以我想知道当我单击输入字段中的空格键时如何在每个单词之间添加 + 键。包含 APi key ,因此任何人都可以检查错误;

const url = 'http://www.omdbapi.com/?s=';
const apiId = '&apikey=gjhytjghjhg';
const input = document.querySelector('input');
const movieDtl = document.querySelector('.movie-detail');
const ul = document.querySelector('ul');
const xhr = new XMLHttpRequest();
let movieId = [];
let inputsearch;
let object;
let movieList;
let movieDetail;

input.addEventListener('keyup', getmovies);

function getmovies(e){
e.preventDefault();
if(e.keyCode === 13){
inputsearch = input.value;
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
getdatalist();
}
}
xhr.open('GET', url + inputsearch + apiId, true);
xhr.send();
}
}

function getdatalist(){
object = JSON.parse(xhr.response);
movieList = object.Search;
movieList.forEach(function(e){
movieId.push(e.imdbID);
let list = document.createElement('li');
movieDetail = '<img src="'+e.Poster+'" alt="Poster"><h4>'+e.Title+'</h4><button>View More</button>';
list.innerHTML = movieDetail;
ul.appendChild(list);
})
getdata();
}

function getdata() {
let vwbtn = document.querySelectorAll('ul li button');
vwbtn.forEach(function(elem, index){
elem.addEventListener('click', getListDetail.bind(null, index));
})
}


function getListDetail(index){
const secondurl = 'http://www.omdbapi.com/?i=';
const mId = movieId[index];
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200){
let secObject = JSON.parse(xhr.response);
let detailList = '<div class="movie-detail-inside"><img class="movie-poster" src="'+secObject.Poster+'" alt="Poster"><div id="detail-wrap"><div class="layer-1"><h5>'+secObject.Type +' '+ '-'+' '+ secObject.Year+'</h5><h5> Released'+' '+secObject.Released+'</h5><h5>'+secObject.Runtime+'</h5><h5>'+secObject.Genre+'</h5><div><img src="star.png" alt="star icon"><h5>'+secObject.imdbRating+'</h5></div></div><h2>'+secObject.Title+'</h2><div class="layer-2"><h4>Production '+' '+'<span>'+secObject.Production+'</span></h4><h4> Director '+' '+'<span>'+secObject.Director+'</span></h4><h4> Writer '+' '+'<span>'+secObject.Writer+'</span></h4><h4> Actors '+' '+'<span>'+secObject.Actors+'</span></h4><h4 class="storyText">Story <br> <span>'+secObject.Plot+'</span></h4></div><div class="layer-3"><h4>'+secObject.Language+'</h4><h4>'+secObject.Country+'</h4><button onclick="exitdetail()">Exit</button></div></div></div>';

movieDtl.innerHTML = detailList;
movieDtl.style.transform = 'scale(1)';
}
}

xhr.open('GET', secondurl + mId + apiId, true);
xhr.send();
}


function exitdetail(){
movieDtl.style.transform = 'scale(0)';

}

HTML

<div id="search-div">
<input type="text" placeholder="Search movie">
</div>

<ul id="movie-showcase">
</ul>

<div class="movie-detail">

</div>

最佳答案

您需要对 inputsearch 中的值进行一些 URL 编码。这是通过调用encodeURIComponent 来完成的。请参阅下面的代码:

For reference URL encoding of a space is either '+' or '%20'.

const url = 'http://www.omdbapi.com/?apikey=bcfe7e46&s=';
const input = document.querySelector('input');
const movieDtl = document.querySelector('.movie-detail');
const ul = document.querySelector('ul');
const xhr = new XMLHttpRequest();
let movieId = [];
let inputsearch;
let object;
let movieList;
let movieDetail;

input.addEventListener('keyup', getmovies);

function getmovies(e){
e.preventDefault();
if(e.keyCode === 13) {
inputsearch = input.value;
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
getdatalist();
}
}

var newUrl = url + encodeURIComponent(inputsearch);
console.log(newUrl);
xhr.open('GET', newUrl, true);
xhr.send();
}
}

function getdatalist(){
object = JSON.parse(xhr.response);
movieList = object.Search;
movieList.forEach(function(e){
movieId.push(e.imdbID);
let list = document.createElement('li');
movieDetail = '<img src="'+e.Poster+'" alt="Poster"><h4>'+e.Title+'</h4><button>View More</button>';
list.innerHTML = movieDetail;
ul.appendChild(list);
})
getdata();
}

function getdata() {
let vwbtn = document.querySelectorAll('ul li button');
vwbtn.forEach(function(elem, index){
elem.addEventListener('click', getListDetail.bind(null, index));
})
}


function getListDetail(index){
const secondurl = 'http://www.omdbapi.com/?i=';
const mId = movieId[index];
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200){
let secObject = JSON.parse(xhr.response);
let detailList = '<div class="movie-detail-inside"><img class="movie-poster" src="'+secObject.Poster+'" alt="Poster"><div id="detail-wrap"><div class="layer-1"><h5>'+secObject.Type +' '+ '-'+' '+ secObject.Year+'</h5><h5> Released'+' '+secObject.Released+'</h5><h5>'+secObject.Runtime+'</h5><h5>'+secObject.Genre+'</h5><div><img src="star.png" alt="star icon"><h5>'+secObject.imdbRating+'</h5></div></div><h2>'+secObject.Title+'</h2><div class="layer-2"><h4>Production '+' '+'<span>'+secObject.Production+'</span></h4><h4> Director '+' '+'<span>'+secObject.Director+'</span></h4><h4> Writer '+' '+'<span>'+secObject.Writer+'</span></h4><h4> Actors '+' '+'<span>'+secObject.Actors+'</span></h4><h4 class="storyText">Story <br> <span>'+secObject.Plot+'</span></h4></div><div class="layer-3"><h4>'+secObject.Language+'</h4><h4>'+secObject.Country+'</h4><button onclick="exitdetail()">Exit</button></div></div></div>';

movieDtl.innerHTML = detailList;
movieDtl.style.transform = 'scale(1)';
}
}

xhr.open('GET', secondurl + mId + apiId, true);
xhr.send();
}


function exitdetail(){
movieDtl.style.transform = 'scale(0)';

}
<div id="search-div">
<input type="text" placeholder="Search movie">
</div>

<ul id="movie-showcase">
</ul>

<div class="movie-detail">

</div>

关于javascript - 每次用户单击输入中的空格键时如何添加 + 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53617060/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com