You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:基于两个下拉框的搜索系统及表格按Cena(price)排序实现

别慌,这个需求其实挺典型的,咱们一步步拆开来做就好。我给你整理了一个完整的示例,包含HTML结构、双下拉搜索逻辑和价格排序功能,你可以跟着改改适配你的实际数据:

第一步:搭建基础HTML结构

先把页面的骨架搭起来——两个下拉框、可选的搜索按钮,还有要操作的表格。注意给关键元素加id,方便后续JS获取:

<!-- 搜索区域 -->
<div class="search-container">
  <label for="filter1">第一个筛选条件:</label>
  <select id="filter1">
    <option value="">全部</option>
    <option value="电子产品">电子产品</option>
    <option value="服饰">服饰</option>
    <option value="家居">家居</option>
  </select>

  <label for="filter2" style="margin-left: 10px;">第二个筛选条件:</label>
  <select id="filter2">
    <option value="">全部</option>
    <option value="新品">新品</option>
    <option value="热销">热销</option>
    <option value="折扣">折扣</option>
  </select>

  <button id="searchBtn" style="margin-left: 10px;">搜索</button>
</div>

<!-- 数据表格 -->
<table id="dataTable" border="1" cellpadding="8" cellspacing="0" style="margin-top: 20px;">
  <thead>
    <tr>
      <th>商品名称</th>
      <!-- 给价格表头加点击事件,触发排序 -->
      <th onclick="sortByCena()" style="cursor: pointer;">Cena 🔽</th>
      <th>分类</th>
      <th>标签</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>无线耳机</td>
      <td>999</td>
      <td>电子产品</td>
      <td>新品</td>
    </tr>
    <tr>
      <td>纯棉T恤</td>
      <td>199</td>
      <td>服饰</td>
      <td>折扣</td>
    </tr>
    <tr>
      <td>智能台灯</td>
      <td>299</td>
      <td>家居</td>
      <td>热销</td>
    </tr>
    <tr>
      <td>机械键盘</td>
      <td>599</td>
      <td>电子产品</td>
      <td>热销</td>
    </tr>
  </tbody>
</table>
第二步:实现双下拉框搜索功能

接下来写JS逻辑,监听搜索按钮的点击(或者下拉框的change事件实现实时搜索),获取两个下拉框的选中值,然后过滤表格里的行:

// 获取DOM元素
const filter1 = document.getElementById('filter1');
const filter2 = document.getElementById('filter2');
const searchBtn = document.getElementById('searchBtn');
const tableBody = document.getElementById('dataTable').querySelector('tbody');
const allRows = Array.from(tableBody.querySelectorAll('tr')); // 把所有行转成数组方便操作

// 搜索函数
function handleSearch() {
  const value1 = filter1.value.trim();
  const value2 = filter2.value.trim();

  allRows.forEach(row => {
    // 获取当前行的分类和标签(对应两个下拉框的筛选字段,你可以根据自己的表格调整索引)
    const category = row.cells[2].textContent.trim();
    const tag = row.cells[3].textContent.trim();

    // 判断是否匹配筛选条件:如果下拉框选了"全部"(空值),就跳过该条件的判断
    const matchesFilter1 = !value1 || category === value1;
    const matchesFilter2 = !value2 || tag === value2;

    // 显示/隐藏行
    row.style.display = matchesFilter1 && matchesFilter2 ? '' : 'none';
  });
}

// 绑定事件:点击搜索按钮触发搜索,也可以给下拉框加change事件实现实时搜索
searchBtn.addEventListener('click', handleSearch);
// 可选:实时搜索
// filter1.addEventListener('change', handleSearch);
// filter2.addEventListener('change', handleSearch);
第三步:实现按Cena排序功能

给价格表头加点击事件,点击时切换升序/降序排列表格行。注意要把价格字符串转成数字再比较,避免字符串排序的问题:

// 记录当前排序方向:true为升序,false为降序
let isAscending = false;

function sortByCena() {
  // 切换排序方向
  isAscending = !isAscending;

  // 对行数组进行排序
  const sortedRows = [...allRows].sort((a, b) => {
    // 获取两行的价格,转成数字
    const priceA = parseFloat(a.cells[1].textContent);
    const priceB = parseFloat(b.cells[1].textContent);

    // 根据排序方向返回比较结果
    return isAscending ? priceA - priceB : priceB - priceA;
  });

  // 清空表格体,然后把排序后的行重新添加进去
  tableBody.innerHTML = '';
  sortedRows.forEach(row => tableBody.appendChild(row));

  // 更新表头的箭头,提示当前排序方向
  const cenaTh = document.querySelector('th[onclick="sortByCena()"]');
  cenaTh.textContent = isAscending ? 'Cena 🔼' : 'Cena 🔽';
}
最后整合与适配

把上面的HTML和JS放在一起就能运行了。你需要根据自己的实际情况调整:

  • 下拉框的选项内容,对应你要筛选的字段
  • 表格里的列索引(比如你的Cena列在第几个,筛选的字段对应哪一列)
  • 如果价格带货币符号(比如999 zł),记得在排序时先去掉符号再转数字,比如:parseFloat(a.cells[1].textContent.replace(/[^0-9.]/g, ''))

这样一套下来,双下拉搜索和价格排序的功能就都实现啦,有问题再随时问~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:25:36