You need to enable JavaScript to run this app. 基于jQuery getJSON的赛事列表加载及选手信息展示开发需求
优惠活动
大模型
产品
解决方案
定价
更多

基于jQuery getJSON的赛事列表加载及选手信息展示开发需求

Solution for Marathon Race Data Implementation

Got it, let's walk through building this feature step by step with clean, modern code that meets all your requirements:

1. Basic HTML Structure

First, set up the button and results container in your HTML:

<button id="fetchDataBtn">获取数据</button>
<div id="getResults"></div>
<div id="runnerDisplay"></div> <!-- Container to show selected race's runners -->

The extra runnerDisplay div gives us a dedicated spot to show the selected race's participants—you can adjust its placement if needed.

2. JavaScript Logic

Add this script to handle data fetching, list rendering, and click interactions:

// Grab DOM elements we'll need
const fetchButton = document.getElementById('fetchDataBtn');
const resultsContainer = document.getElementById('getResults');
const runnerContainer = document.getElementById('runnerDisplay');

// Handle "获取数据" button click
fetchButton.addEventListener('click', async () => {
  try {
    // Fetch race data from the specified URL
    const raceResponse = await fetch('http://itweb.fvtc.edu/wetzel/marathon/races/');
    if (!raceResponse.ok) throw new Error('Failed to load race data');
    
    const races = await raceResponse.json();
    
    // Clear old results before showing new ones
    resultsContainer.innerHTML = '';
    
    // Build the race list
    const raceList = document.createElement('ul');
    races.forEach(race => {
      const listItem = document.createElement('li');
      listItem.id = race.id; // Assign race ID as the li's id attribute (e.g., <li id="1">)
      listItem.textContent = race.name; // Show the race name
      
      // Add click handler to load runners for this race
      listItem.addEventListener('click', async () => {
        try {
          // Use the li's id to fetch the corresponding runners
          const runnerResponse = await fetch(`http://itweb.fvtc.edu/wetzel/marathon/races/${listItem.id}/runners`);
          if (!runnerResponse.ok) throw new Error('Failed to load runner data');
          
          const runners = await runnerResponse.json();
          
          // Render the runner names
          runnerContainer.innerHTML = `<h3>${race.name} 参赛选手</h3>`;
          const runnerList = document.createElement('ul');
          runners.forEach(runner => {
            const runnerItem = document.createElement('li');
            runnerItem.textContent = runner.name;
            runnerList.appendChild(runnerItem);
          });
          runnerContainer.appendChild(runnerList);
        } catch (err) {
          runnerContainer.textContent = `加载选手数据失败: ${err.message}`;
        }
      });
      
      raceList.appendChild(listItem);
    });
    
    resultsContainer.appendChild(raceList);
  } catch (err) {
    resultsContainer.textContent = `加载赛事数据失败: ${err.message}`;
  }
});

Key Details Breakdown:

  • Async/Await for Clean Code: We use modern async/await syntax to handle asynchronous data fetching, making the code easier to read and debug.
  • Race List Rendering: Each race gets an <li> with its ID set as the element's id attribute (exactly like your example: <li id="1">Storm King Run</li>).
  • Race Click Interaction: When a user clicks a race, we use the <li>'s id to fetch the corresponding runner data. We assume the runner endpoint follows the pattern races/{raceId}/runners—adjust this URL if your actual endpoint uses a different structure.
  • Error Handling: Try/catch blocks ensure users see clear error messages if something goes wrong with network requests, instead of broken or missing content.

3. Optional Styling (For Better UX)

Add some basic CSS to make the interface more user-friendly:

#fetchDataBtn {
  padding: 8px 16px;
  cursor: pointer;
  margin-bottom: 1rem;
}

#getResults ul {
  list-style: none;
  padding: 0;
}

#getResults li {
  padding: 8px;
  border: 1px solid #eee;
  margin-bottom: 4px;
  cursor: pointer;
}

#getResults li:hover {
  background-color: #f5f5f5;
}

#runnerDisplay {
  margin-top: 1rem;
  padding: 1rem;
  border-top: 1px solid #ddd;
}

This implementation covers every requirement you outlined: fetching race data on button click, displaying the list with proper ID attributes, and loading runner details using the race ID from the <li> element.

内容的提问来源于stack exchange,提问作者Allison Gandara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:03:58