javascript - 从onject生成元素后,如何根据单击的相对元素获取对象
问题描述
我正在创建一个从数据库中检索到的产品表。目前,我能够接收数据并将其显示为输入表。我想要做的是创建一个方法,在单击输入时获取产品并将其存储在变量中。
下面是生成输入表的代码:
<table style="margin:0 auto;">
<% products.forEach(function(product) { %>
<tr>
<form action="/product" method="POST">
<td><input class="pid" type="submit" name="pid" value="<%= product.product_id%>"
style="width: 100px;"></input>
<td><input class="product-option" type="submit" name="product"
value="<%= product.name%>" style="width: 500px;"></input>
</form>
</tr>
<% }); %>
</table>
whereproducts
被传递给页面的对象如下所示:
例子:
RowDataPacket {
name: 'Vinegar - Tarragon',
product_id: 960,
img_src: 'https://www.sandu.in/image/cache/catalog/product/no-product-800x800.png',
description: 'Seamless contextually-based utilisation',
cost: 168.1,
date_created: 2021-05-31T12:00:00.000Z,
date_modified: null,
date_deleted: null,
category_id: 960,
inventory_id: 960,
discount_id: 960
}
目前,当表单发布时,我有一个方法,它按名称获取元素,product,
如下所示:
router.post('/product', function (req, res) {
if (session.user) { //Session isn't relevant
var body = ''
req.on('data', function (data) {
body += data;
if (body.length > 1e6)
req.connection.destroy();
});
req.on('end', function () {
var result = qs.parse(body);
console.log(result) //Returns "[Object: null prototype] { product: 'Vinegar - Tarragon' }"
console.log(result.product); //Returns "Vinegar - Tarragon"
});
} else {
res.redirect('/', {
title: "Login"
});
}
});
有没有一种方法可以在单击输入时获取整个对象(如示例中所示),而不是仅获取产品名称?
解决方案
推荐阅读
- excel - Excel:一列中的数字总和,每个单元格中的数据有两个由斜线分隔的数字(例如:12/24)
- python-3.x - 对python中的对象进行排序并对具有相同值的对象进行分组?
- javascript - Javascript:如何在异常时记录函数参数
- windows - 如何在 WiX 中有条件地设置属性或 RegistryValue?
- elasticsearch - spring data elasticsearch如何使用子关键字字段创建文本字段
- python - 最大递归,如何修复
- tinymce - 如何从 TinyMce Web 组件编辑器中删除 POWERED BY TINY
- function - 为什么我不能在 Kotlin 中更新函数参数的值?
- shiro - ShiroWebModule 中已经配置了 org.apache.shiro.web.filter.authc.LogoutFilter
- javascript - 学习如何构建一个重新生成的数组列表