<script>
// 表单提交事件处理函数const form = document.querySelector('form');form.addEventListener('submit', (event) => {event.preventDefault();// 获取表单数据const location = document.querySelector('location').value;const date = document.querySelector('date').value;// 验证表单数据if (!location || !date) {alert('请填写完整信息');return;}// 发送请求获取数据const url = 'api.php?location=' + location + '&date=' + date;fetch(url).then(response => response.json()).then(data => displayResults(data)).catch(error => console.error(error));});// 数据展示函数function displayResults(data) {// 获取结果容器const results = document.querySelector('results');// 清空结果容器results.innerHTML = '';// 创建标题元素const title = document.createElement('h2');title.classList.add('results-title');title.textContent = '查询结果:';// 将标题添加到结果容器中results.appendChild(title);// 创建列表元素
const list = document.createElement('ul');list.classList.add('results-list');// 循环遍历数据for (const item of data) {// 创建列表项元素const li = document.createElement('li');li.classList.add('results-item');// 创建链接元素const a = document.createElement('a');a.href = item.link;// 创建标题元素const h3 = document.createElement('h3');h3.classList.add('results-item-title');h3.textContent = item.name;// 创建价格元素const p = document.createElement('p');p.classList.add('results-item-price');p.textContent = item.price;// 将元素添加到列表项中a.appendChild(h3);a.appendChild(p);li.appendChild(a);// 将列表项添加到列表中list.appendChild(li);}// 将列表添加到结果容器中results.appendChild(list);}
script>
郑重声明:本文由:
成都旅游于(2024-10-18)发表了关于
附近宾馆住宿查询500米内价格 (附近宾馆住宿查询500米)的文章。如转载请注明出处!部分文章来源于网络,仅作为参考,如果网站中图片和文字侵犯了您的版权,请联系我们处理!