查找最近的门店
门店列表
javascript
// script.js
const form = document.getElementById('form-search');
const listStores = document.getElementById('list-stores');form.addEventListener('submit', (e) => {e.preventDefault();const address = document.getElementById('input-address').value;if (!address) {alert('请输入地址!');return;}fetch(`${address}`, {headers: {'Content-Type': 'application/json',},}).then((res) => res.json()).then((data) => {const stores = data.data.stores;if (stores.length === 0) {listStores.innerHTML = '
附近没有门店
';} else {for (const store of stores) {const li = document.createElement('li');li.innerHTML= `
${store.name}
${store.address}
电话:${store.phone}
营业时间:${store.hours}
`;listStores.appendChild(li);}}}).catch((err) => {console.error(err);alert('请求失败,请重试!');});
});