如何从流星动态生成的表单中获取文本框的值,而文本框的数量不固定

How to get values of textbox from dynamically generated form in meteor, while the number of textbox is not fixed

本文关键字:文本 获取 动态 流星 表单 取文本      更新时间:2023-09-26

我想从动态生成的表单中获取文本框的值并存储在数据库中。文本框的数量不固定。表单在模板中

    <template name="product">
     <input type="text" id="txt1">
     <input type="text" id="txt2">
     .......
     .......
     <button id="CreateNewProduct">Create new product</button>
    </template>

我想在"product"集合中插入文本框值。我在这里展示了两个文本框供参考。可能是3 4 5 ......最多n个。也可以包含复选框和单选。

你能建议我如何进行这项任务吗?

您需要遍历现有的输入元素,并将它们对应的id s与它们在表单中作为Product集合特定属性的引用的n th进行匹配。

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);
    }
})