You need to enable JavaScript to run this app. Node.js+EJS多页表单下拉联动:首表单options联动次表单Station_Type选项
优惠活动
大模型
产品
解决方案
定价
更多

Node.js+EJS多页表单下拉联动:首表单options联动次表单Station_Type选项

多页表单跨页下拉联动实现调整方案

现有代码核心问题

  • 第一页提交options入库后,直接跳转到无关路由,未将提交的options值传递到第二页表单流程,第二页无关联过滤依据
  • 第二页下拉查询的数据源完全错误:本应查询station表中与当前options映射的Station_Type值,实际查询了style表的全量options字段,既没有过滤条件,取的字段也不对
  • 存在HTML语法错误:Station_Type下拉的<select>标签重复声明了两次name属性
  • 所有数据库查询采用字符串拼接写法,存在SQL注入风险

分步代码调整

1. 第一页提交逻辑改造

原逻辑入库后直接跳转无参数页面,改造为入库成功后携带刚提交的options值,跳转到第二页表单路由,作为下拉过滤的关联依据:

addStyle: (req, res) => {
    let options = req.body.options.trim();
    // 基础参数校验
    if (!options) return res.status(400).send('options输入不能为空');
    // 参数化查询替代字符串拼接
    let query2 = "INSERT INTO `style` (options) VALUES (?)";
    db.query(query2, [options], (err, result) => {
        if (err) {
            return res.status(500).send(err);
        }
        // 携带options值跳转第二页,URL参数做编码处理避免特殊字符乱码
        res.redirect(`/add-player?selected_style=${encodeURIComponent(options)}`);
    });
},

2. 第二页表单路由查询逻辑改造

原逻辑全量查询style表的options值,改造为从URL参数获取关联的options值,关联查询station表的映射数据作为下拉数据源:

addPlayerPage: (req, res) => {
    // 从URL参数解码获取上一页传递的关联options值
    const selectedStyle = req.query.selected_style ? decodeURIComponent(req.query.selected_style.trim()) : null;
    // 无关联值时做兜底提示
    if (!selectedStyle) {
        return res.render('add-player.ejs', {
            title: "Welcome | Add a new transaction",
            message: '请先从第一页提交有效options值进入',
            drecord: [],
            currentStyle: ''
        });
    }
    // 关联查询station表:仅返回与当前options匹配的Station_Type记录
    let myQuery = "SELECT Station_Type FROM station WHERE options = ?";
    db.query(myQuery, [selectedStyle], function(err, result) {
        if (err) {
            return res.status(500).send(err);
        }
        res.render('add-player.ejs', { 
            title: "Welcome | Add a new transaction",
            message: '',
            drecord: result,
            currentStyle: selectedStyle // 将当前关联的options值传到前端做提示
        });
    });
},

3. 第二页表单EJS渲染逻辑改造

原循环渲染的是drecord[i].options,与新的查询字段不匹配,同时修复select标签重复写name属性的语法错误:

<!-- 展示当前关联的options值,避免用户认知混乱 -->
<% if (currentStyle) { %>
<div class="alert alert-info">当前关联样式:<%= currentStyle %></div>
<% } %>
<div class="form-group">
    <label for="Station_Type">Station_Type</label>
    <!-- 删除重复的name属性,仅保留一次声明 -->
    <select name="Station_Type" class="form-control" id="Station_Type" autocomplete="off">
        <option value="">-- 请选择站点类型 --</option>
        <% for(var i=0; i < drecord.length; i++) { %>
            <!-- 渲染station表返回的Station_Type值,替代原来的options字段 -->
            <option value="<%= drecord[i].Station_Type %>"><%= drecord[i].Station_Type %></option>
        <% } %>
        <!-- 无匹配数据时做空状态提示 -->
        <% if (drecord.length === 0) { %>
            <option value="">当前样式无匹配站点类型</option>
        <% } %>
    </select>
</div>

4. 第二页提交入库逻辑改造

原逻辑采用字符串拼接SQL,且未将关联的options值存入lineid表,改造为参数化查询,补全关联字段方便后续追溯:

addPlayer: (req, res) => {
    let Station_Type = req.body.Station_Type.trim();
    let clothes_name = req.body.clothes_name.trim();
    // 获取当前关联的options值,存入lineid表建立关联关系
    const relatedStyle = req.query.selected_style ? decodeURIComponent(req.query.selected_style.trim()) : '';
    // 参数化查询替代字符串拼接
    let query = "INSERT INTO `lineid` (Station_Type, clothes_name, style_options) VALUES (?, ?, ?)"; 
    db.query(query, [Station_Type, clothes_name, relatedStyle], (err, result) => {
        if (err) {
            return res.status(500).send(err);
        }
        res.redirect('/line'); 
    });  
},

流程校验说明

整个链路实现闭环:

  • 第一页提交options值,存入style表完成持久化
  • 携带options值跳转第二页,作为下拉查询的过滤条件
  • 从station表匹配当前options对应的所有Station_Type值,渲染到下拉选项中,实现“输入A仅出A关联站点、输入B仅出B关联站点”的业务要求
  • 第二页表单提交后,将选中的站点值、关联options值等存入lineid表,完成数据存储

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.24 11:15:03