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

技术需求:配置两个下拉选择框、输入框及按钮,解析给定JS代码

Building Your UI & Parsing the JavaScript Logic

Let's tackle this step by step—first putting together the UI elements you need, then breaking down that JavaScript code to understand how it works.

1. The UI Structure (HTML)

Here's a complete HTML setup with two dropdowns, an input field + button, and the ordered list your JavaScript interacts with:

<!-- 两个下拉选择框 -->
<select id="institutionSelect" onchange="changeInstitution(this.selectedIndex)">
  <option value="all">All Institutions</option>
  <option value="inst1">Institution 1</option>
  <option value="inst2">Institution 2</option>
</select>

<select id="categorySelect">
  <option value="all">All Categories</option>
  <option value="cat1">Category A</option>
  <option value="cat2">Category B</option>
</select>

<!-- 输入框与按钮 -->
<div class="filter-group">
  <input type="text" id="myInput" placeholder="Search items...">
  <button onclick="submit()">Search</button>
</div>

<!-- 待筛选的列表 -->
<ol id="myList">
  <li><a href="#">Item 1 (Institution 1, Category A)</a></li>
  <li><a href="#">Item 2 (Institution 2, Category B)</a></li>
  <li><a href="#">Item 3 (Institution 1, Category B)</a></li>
  <li><a href="#">Item 4 (Institution 2, Category A)</a></li>
</ol>

I added a second dropdown with a category example, and hooked the first dropdown directly to your changeInstitution function via the onchange attribute for immediate interaction.

2. Parsing the JavaScript Logic

Let's unpack the code you provided, piece by piece:

The submit() Function (Core List Filtering)

This is your search/filter workhorse—here's what each part does:

var input, filter, ol, li, a, i; 
function submit() { 
  input = document.getElementById("myInput"); 
  filter = input.value.toUpperCase(); // Convert input to uppercase for case-insensitive search
  ol = document.getElementById("myList"); 
  li = ol.getElementsByTagName("li"); // Grab all list items in the ordered list

  // Loop through every list item
  for (i = 0; i < li.length; i++) { 
    a = li[i].getElementsByTagName("a")[0]; // Get the link inside each list item
    // Check if the link's text contains the filter text
    if (a.innerHTML.toUpperCase().indexOf(filter) > -1) { 
      li[i].style.display = ""; // Show the item if there's a match
    } else { 
      li[i].style.display = "none"; // Hide the item if no match
    } 
  } 
}
  • DOM Operations: It uses getElementById to target the input field and list, then getElementsByTagName to loop through list items and their inner links.
  • Filter Logic: Converts both the input text and list item text to uppercase to make the search case-insensitive. If the list item's text includes the search term, it stays visible; otherwise, it's hidden.

The changeInstitution(index) Function (Partial Code)

Your code cuts off, but from what's there, it's clear this function is meant to filter the list based on the selected institution. I filled in the missing logic to make it functional:

function changeInstitution(index) { 
  if (index == 0) { 
    ol = document.getElementById("myList"); 
    // Show all list items when "All Institutions" is selected
    let items = ol.getElementsByTagName("li");
    for (let item of items) {
      item.style.display = "";
    }
  } else {
    // For other indices, filter items to only show those from the selected institution
    let ol = document.getElementById("myList");
    let li = ol.getElementsByTagName("li");
    for (let i = 0; i < li.length; i++) {
      let a = li[i].getElementsByTagName("a")[0];
      // Check if the item's text includes the selected institution name (adjust this to match your data)
      if (a.innerHTML.includes(`Institution ${index}`)) {
        li[i].style.display = "";
      } else {
        li[i].style.display = "none";
      }
    }
  }
}

You can tweak the institution check to match how your actual list items are labeled (e.g., using data attributes instead of plain text if you want cleaner code).

3. Full Working Code (With Basic Styling)

Add this CSS to make the UI look polished:

.filter-group {
  margin: 10px 0;
}
#myInput {
  padding: 8px;
  width: 250px;
}
button {
  padding: 8px 16px;
  cursor: pointer;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
}
select {
  margin-right: 10px;
  padding: 8px;
  border-radius: 4px;
}
li {
  margin: 5px 0;
}

Put it all together, and you'll have a fully functional UI with dropdown filtering and keyword search.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:22:16