ejs - How do I update which option is selected based on EJS
问题描述
I have a express server that passes in information from a mongo db into a ejs file. I am trying to update an item in the db. I want to have the current item information entered in as placeholders.
Here is the ejs code I'm not sure how to select one option based on the info from the db.
<select name="category" id="Category" placeholder="<%= product.category %>">
<option value="vegetable">Vegetable</option>
<option value="fruit">Fruit</option>
<option value="dairy">Dairy</option>
</select>
I understand that to have an option as the placeholder you generally have to use <option selected>
Any suggestions are much appreciated.
解决方案
使用三元运算符
<select name="category" id="Category">
<option value="vegetable" <%=product.category==='vegetable' ? 'selected' : '' %>>Vegetable</option>
<option value="fruit" <%=product.category==='fruit' ? 'selected' : '' %>>Fruit</option>
<option value="dairy" <%=product.category==='dairy' ? 'selected' : '' %>>Dairy</option>
</select>
推荐阅读
- javascript - HTML 更改输入元素的值,当值元素已经为空时
- python - 如何识别和突出熊猫数据框每一行中的异常值
- javascript - 如何操作 jsonl 对象
- python-3.x - 我的 f' 字符串是否禁用了我的 sorted() 函数?
- r - 使用 tesseract 进行 OCR 后提取一段文本
- android-studio - 单击每个列表项,如何在另一个活动中播放该项目的歌曲?
- webhooks - Google Cloud Build 的 POST 正文 - Webhook 触发器
- sql - 如何根据具有更新值的列过滤源表中的更新?只需要为一个特定的列更新具有新值的行
- html - 无法分别添加滚动到内容和侧栏 div
- excel - 在上下文单元菜单中放置一个按钮