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

标签 javascript html meteor

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

    <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/

相关文章:

javascript - 创建 excel 文件并使用 ExcelJS 写入

javascript - 图像预加载不起作用

html - css,锁定html标签里面有图片

html - CSS 使用 :target selector 更改整个表格行的背景颜色

meteor - 将参数传递到模板而不覆盖数据上下文

javascript - $ ('document' ).ready() 函数在ajax响应后不起作用

javascript - 返回值返回为未定义

javascript - Google Plus 一键速度。鼠标悬停加载

javascript - 单击后如何测试显示元素?

javascript - MongoDB:如何订阅大数组的小子集?