You need to enable JavaScript to run this app. 基于Bootstrap5和Flask如何创建自定义选择框并插入Bootstrap Card?
优惠活动
大模型
产品
解决方案
定价
更多

基于Bootstrap5和Flask如何创建自定义选择框并插入Bootstrap Card?

实现方案

逻辑拆分

这个效果不需要复杂的第三方组件,拆分为3个模块实现即可:

  • 带搜索功能的Bootstrap 5下拉选择器
  • 模拟输入框样式的卡片容器
  • 可删除的精简Bootstrap Card组件

前端核心代码(Bootstrap 5 + 原生JS)

HTML结构

<!-- 卡片容器:模拟输入框样式 -->
<div class="border rounded p-2 mb-2 min-h-[44px]" id="selected-card-container">
  <!-- 选中的卡片会动态插入到此处 -->
</div>

<!-- 可搜索下拉组件 -->
<div class="dropdown">
  <button class="btn btn-outline-secondary dropdown-toggle w-100 text-start" type="button" data-bs-toggle="dropdown" aria-expanded="false">
    选择选项
  </button>
  <ul class="dropdown-menu w-100 p-2">
    <!-- 下拉内搜索框 -->
    <li class="mb-2">
      <input type="text" class="form-control" id="dropdown-search-input" placeholder="搜索选项...">
    </li>
    <li><hr class="dropdown-divider"></li>
    <!-- 下拉选项列表,可后端动态渲染 -->
    <li><button class="dropdown-item option-btn" data-card-title="选项A" data-card-desc="选项A附加说明">选项A</button></li>
    <li><button class="dropdown-item option-btn" data-card-title="选项B" data-card-desc="选项B附加说明">选项B</button></li>
    <li><button class="dropdown-item option-btn" data-card-title="选项C" data-card-desc="选项C附加说明">选项C</button></li>
  </ul>
</div>

交互JS

// 1. 下拉选项搜索过滤
document.getElementById('dropdown-search-input').addEventListener('input', (e) => {
  const key = e.target.value.trim().toLowerCase();
  document.querySelectorAll('.option-btn').forEach(btn => {
    btn.style.display = btn.textContent.toLowerCase().includes(key) ? 'block' : 'none';
  });
});

// 2. 选中选项插入卡片
document.querySelectorAll('.option-btn').forEach(btn => {
  btn.addEventListener('click', () => {
    // 从自定义属性拿卡片数据
    const cardTitle = btn.dataset.cardTitle;
    const cardDesc = btn.dataset.cardDesc;
    // 生成精简卡片HTML
    const card = `
      <div class="card d-inline-flex m-1" style="width: 130px;">
        <div class="card-body p-2">
          <button type="button" class="btn-close btn-xs float-end remove-card-btn"></button>
          <h6 class="card-title mb-1 fs-sm">${cardTitle}</h6>
          <p class="card-text text-muted fs-xs mb-0">${cardDesc}</p>
        </div>
      </div>
    `;
    // 插入到容器
    document.getElementById('selected-card-container').insertAdjacentHTML('beforeend', card);
    // 重置下拉状态
    document.getElementById('dropdown-search-input').value = '';
    bootstrap.Dropdown.getInstance(document.querySelector('[data-bs-toggle="dropdown"]')).hide();
  });
});

// 3. 删除选中的卡片
document.getElementById('selected-card-container').addEventListener('click', (e) => {
  if (e.target.classList.contains('remove-card-btn')) {
    e.target.closest('.card').remove();
  }
});

Flask后端对接

动态渲染下拉选项

如果选项数据是动态从后端获取,直接在路由中传递数据,用Jinja模板循环渲染即可:

# app.py
from flask import Flask, render_template, request

app = Flask(__name__)

# 模拟从数据库获取选项数据
def get_options():
    return [
        {"title": "选项A", "desc": "选项A附加说明"},
        {"title": "选项B", "desc": "选项B附加说明"},
        {"title": "选项C", "desc": "选项C附加说明"}
    ]

@app.route('/')
def index():
    options = get_options()
    return render_template('index.html', options=options)

模板中替换下拉选项部分的代码:

{% for opt in options %}
<li><button class="dropdown-item option-btn" data-card-title="{{ opt.title }}" data-card-desc="{{ opt.desc }}">{{ opt.title }}</button></li>
{% endfor %}

提交选中卡片数据

如果需要把选中的卡片内容提交到后端,提交前遍历容器内的卡片,把数据组装成数组传递即可:

// 提交数据示例
function submitData() {
  const selected = [];
  document.querySelectorAll('#selected-card-container .card').forEach(card => {
    selected.push({
      title: card.querySelector('.card-title').textContent,
      desc: card.querySelector('.card-text').textContent
    });
  });
  // 用fetch或者表单提交selected数组到后端
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.22 02:12:08