如何从 express 的 html 下拉列表中获取选定的值?

How to get selected value from html dropdown in express?

我在我的项目中使用 html 下拉菜单。我如何在快速服务器中获取 select 值?

<div class="mb-3">
            <label for="kk" class="form-label">Designation</label>
            <select class="form-select" name="picker" aria-label="Default select example" id="kk">
                <option selected>Select</option>
                <option value="1">Proffesor</option>
                <option value="2">Associate Proffessor</option>
                <option value="3">Lab Assistant</option>
            </select>
        </div>

在我的 post 请求处理方法中,我使用此代码获取值:

const f = req.body.picker;

这给我的是下拉列表中 selected 值的索引,如 0、1、2 等,而不是像教授、副教授、实验室助理等实际值。

当您发送请求时,您实际上得到了 value 属性中的内容。对于你想要的数据,你可以这样做:

<div class="mb-3">
            <label for="kk" class="form-label">Designation</label>
            <select class="form-select" name="picker" aria-label="Default select example" id="kk">
                <option value="" selected>Select</option>
                <option value="Proffesor">Proffesor</option>
                <option value="Associate Proffessor">Associate Proffessor</option>
                <option value="Lab Assistan">Lab Assistant</option>
            </select>
        </div>

这就是您将得到的:

const f = req.body.picker; // -> "" or "Proffesor" or "Associate Proffessor" or "Lab Assistan"