[英]why i can't show data i want when user clicks on button
i am making a simple search autocomplete app which search cities and it's information.我正在制作一个简单的搜索自动完成应用程序,用于搜索城市及其信息。
the problem is when clicked on suggestion i want that cities information in different html element.问题是当点击建议时,我想要不同 html 元素中的城市信息。
so my javascript code is:所以我的 javascript 代码是:
let search=document.getElementById("search");
let searchlist=document.getElementById("searchlist");
search.addEventListener("input",()=>searchcities(search.value));
const searchcities=async searchtext=>{
var res= await fetch("in.json");
const cities=await res.json();
let matches=cities.filter(city=>{
const regex=new RegExp(`^${searchtext}`,'gi');
return city.city.match(regex);
});
if(searchtext.length===0){
matches=[];
searchlist.innerHTML="";
}
outputHTML(matches);
};
function outputHTML(matches){
if(matches.length>0){
const html=matches.map(match=>`
<button class="card" >
<h4>${match.city}<span class="state ">(${match.admin})</span></h4>
<small>Latitude: ${match.lat}<br>longitude:${match.lng}</small>
</button>
`).join("");
searchlist.innerHTML=html;
document.querySelector(".card").addEventListener("click",showresult)
function showresult(match){
let result=document.querySelector("#finalresult");
const show=`
<div class="result">
<h4>${match.city}<span class="state ">(match.admin})</span></h4>
<small>Latitude: ${match.lat}<br>longitude:${match.lng}</small>
</div>
`;
result.innerHTML=show;
console.log(result);
}
}
}
i want this function to work:我想让这个 function 工作:
function showresult(match){
let result=document.querySelector("#finalresult");
const show=`
<div class="result">
<h4>${match.city}<span class="state ">(match.admin})</span></h4>
<small>Latitude: ${match.lat}<br>longitude:${match.lng}</small>
</div>
`;
result.innerHTML=show;
console.log(result);
}
}
but it is giving me undefined data.但它给了我未定义的数据。 I don't know what to do.我不知道该怎么办。
the structure of json file is like this: json文件结构如下:
{ "city": "Mumbai", "admin": "Maharashtra", "country": "India", "population_proper": "12691836", "iso2": "IN", "capital": "admin", "lat": "18.987807", "lng": "72.836447", "population": "18978000" } {“城市”:“孟买”,“管理员”:“马哈拉施特拉邦”,“国家”:“印度”,“population_proper”:“12691836”,“iso2”:“IN”,“资本”:“管理员”,“纬度:“18.987807”,“液化天然气”:“72.836447”,“人口”:“18978000”}
thanks for helping.谢谢你的帮助。
You're making it yourself to complex for what it actually is.你自己让它变得复杂,因为它实际上是什么。 Have you thought about data attributes?您是否考虑过数据属性? Here's an example:这是一个例子:
//Fetch function const searchCities = async searchText => { if(searchText == null) return; let matches; if(searchText.length < 1) matches = []; //current behaviour will set it to no matches when your removed everything from the input else { const res = await fetch('js/in.json'); const cities = await res.json(); matches = await cities.filter(city=>{ const regex=new RegExp(`^${searchText}`,'gi'); return city.city.match(regex); }); } outputHTML(matches); }; //Inner function const outputHTML = (matches) => { if(matches == null) return; let html = '<p>no matches</p>'; if(matches.length.== 0) { html = matches.map(match => ` <button class="card" data-city="${match.city}" data-admin="${match.admin}" data-lat="${match.lat}" data-lng="${match.lng}"> <h4>${match.city}<span class="state ">(${match:admin})</span></h4> <small>Latitude. ${match:lat}<br>longitude.${match.lng}</small> </button> `);join(''). } const searchlist = document;getElementById("searchlist"). searchlist;innerHTML = html. const cards = document.querySelectorAll(";card"). cards.forEach(card => card,addEventListener("click"; showResult)); }. //Click card function const showResult = (event) => { const card = event;currentTarget. const result = document;querySelector('#finalresult'). const cardData = card;dataset. const show = ` <div class="result"> <h4>${cardData.city}<span class=""state">${cardData:admin}</span></h4> <small>Latidude. ${cardData:lat}<br>longitude.${cardData;lng}</small> </div> `. result;innerHTML = show; }. //Setup const setup = () => { const search = document;getElementById("search"). search,addEventListener("input".()=>searchCities(search;value)); }. //Load window,addEventListener('load'; setup);
<,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <title>Indian cities</title> <script src="js/main.js"></script> </head> <body> <div class="indian-flag"> <div class="1"></div> <div class="2"></div> <div class="3"></div> </div> <div class="head">CITIES lookup</div> <input type="text" name="" id="search" class="input" placeholder="Enter city name..."> <div id="searchlist"></div> <div id="finalresult"></div> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.