【问题标题】:How to get one item from option list如何从选项列表中获取一项
【发布时间】:2019-07-02 15:23:11
【问题描述】:

我正在尝试从 ejs 选项列表中从我的数据库中获取一项。

这是我的代码

<form action="/chords/id" method='POST'>
    <select name="root" id="root">
        <%chords.forEach(function(chord){%>
            <option value="<%=chord.id%>"><%=chord.root%></option>
       <% }) %>
    </select>
    <select name="type" id="type">
        <%chords.forEach(function(chord){%>
            <option value=""><%=chord.type%></option>
       <% }) %>
    </select>
    <select name="extension" id="extension">
        <%chords.forEach(function(chord){%>
            <option value=""><%=chord.extension%></option>
       <% }) %>
    </select>


    <input type="submit" value='SUBMIT'>

</form>

我的数据库有 4 个键:root、type、extension 和 note。我的想法是我从选项列表中选择根、类型和扩展名,然后这应该返回我的和弦音符并在页面上打印 chord.note。我怎样才能做到这一点 ?我应该在路线的表单动作中传递什么?请帮帮我,谢谢

【问题讨论】:

    标签: javascript node.js postgresql express


    【解决方案1】:

    我不了解 ejs,但在 JavaScript 中,我们不能在提交事件时做这样的事情:

    const select = event.target.elements['select']
    const optionValue = select.options[select.selectedIndex].value
    
    console.info('Value:', optionValue)
    

    HTH

    【讨论】:

      【解决方案2】:

      您忘记在 option 的扩展名和类型值上设置 chord.id。

      Check in the example

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多