我想从动态生成的表单中获取文本框的值并存储在数据库中。文本框的数量不固定。表单位于模板内。
<template name="product">
<input type="text" id="txt1">
<input type="text" id="txt2">
.......
.......
<button id="CreateNewProduct">Create new product</button>
</template>
我想在“产品”集合中插入文本框值。 我刚刚在这里展示了两个文本框以供引用。可能是3,4,5,......最多n个数。它还可能包含复选框和单选按钮。
你能建议我如何继续这个任务吗?
最佳答案
您需要遍历现有的输入元素,并将它们相应的 id
与其在表单中的第 n
位置相匹配,作为产品特定属性的引用收藏。
Template.product.events({
"submit form": function(event, template) {
var product = {};
template.$("input").each(function(index) {
product["property"+index] = template.$("input#txt"+index).val();
});
Product.insert(product);
}
})
关于javascript - 如何从 meteor 中动态生成的表单中获取文本框的值,而文本框的数量不固定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30434553/