如何使用包含迭代的模板助手从流星中更新流星的数据

问题描述 投票:1回答:2

我刚刚浏览过meteor.js并做了一些实验。我想使用文本框更新数据。但是该文本框是通过模板助手迭代生成的。。

所以,场景就像我们先输入数据然后检索它,然后我们就可以编辑数据了。所以当我们编辑时

问题是我无法从文本框中获取值。其始终为“未定义”。这是我的html代码:

<body>
    <form class="InsertTEST">
        <input type="text" name="att1" placeholder="fill the att1"/>
        <input type="text" name="att2" placeholder="fill the att3"/>
        <input type="submit" value="submit"/>
    </form>
    <table>
        {{#each a}}
            {{> test}}
        {{/each}}
    </table>
</body>

<template name="test">
    <tr class="testRow">
        <td><input type="checkbox" class="toogle-check"></td>
        <td><input type="text" id="att4" value="{{att1}}"/></td>
        <td><input type="text" id="att5" value="{{att2}}"/></td>
        <td><a href="#">{{att3}}</a></td>
        <td><button class="UpdateTEST">Update</button></td>
        <td><button class="DeleteTEST">Remove</button></td>
    </tr>
</template>

这里是我的js代码:

TEST = new Mongo.Collection('TEST');

if (Meteor.isClient) {
    Template.body.helpers({
        a: function() {
            return TEST.find();
        }
    });

    Template.body.events({
        'submit .InsertTEST' : function(event){

            var _att1 = event.target.att1.value;
            var _att3 = new Date();
            var _att2 = Number(event.target.att2.value) + 10;

            Meteor.call('InsertTEST', _att1, _att2, _att3);

            event.target.att1.value = "";
            event.target.att2.value = "";


            return false;
        }


    });

    Template.test.events({

        'click .UpdateTEST' : function(e,t) {
            var _att4 = $('#att4').val();
            alert(_att4);


            /*
            var query = {
                att1 : _att4,
                att2 : _att5
            };
            */
            TEST.update(this._id, {$set:query});
            return false;
        }

    });

}

if (Meteor.isServer) {
    Meteor.methods({
        'InsertTEST' : function(_att1, _att2, _att3) {

            TEST.insert({
                att1:_att1, att2:_att2
            });

        }

    });
}
javascript jquery meteor textbox updates
2个回答
0
投票

你知道为什么吗?您的HTML中有重复的ID。您多次渲染ID为att4

att5

0
投票

[首先,不要将助手命名为“ a”,您需要输入适当的名称。其次,您不应该在流星中使用body标签。它是由服务器生成的,并且您的模板已加载到其中。

© www.soinside.com 2019 - 2024. All rights reserved.