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

Sha*_*ari 6 html javascript meteor

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

    <template name="product">
     <input type="text" id="txt1">
     <input type="text" id="txt2">
     .......
     .......
     <button id="CreateNewProduct">Create new product</button>
    </template>
Run Code Online (Sandbox Code Playgroud)

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

你能建议我如何继续这项任务吗?

Bek*_*Bek 0

您将需要迭代现有的输入元素,并将其相应的ids 与n表单中的 th 进行匹配,作为 Product 集合的特定属性的参考。

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

})
Run Code Online (Sandbox Code Playgroud)