将复选框更新为 mongo 有时有效,有时无效

checkbox update to mongo is sometime work and sometime won't work

我的代码有时能用,有时不能用

Mongo 有时没更新...

但他的代码是完美的

我做错了什么?

这是我的代码

http://checkboxploblem.meteor.com

https://github.com/codepawn/practice_makes_perfect/tree/master/checkbox_mongo

来源是

https://github.com/EventedMind/class-build-a-multi-page-app-with-iron-meteor-6737880d

您的问题出在 'home.coffee' 文件中的事件:

Template.Home.events
    'click [name=isDone]': (e, tmpl) ->
    id = @_id
    isDone = tmpl.find('input[name=isDone]').checked
    Todos.update {_id: id},
        $set:
            isDone: isDone

您正在将第一个复选框值分配给变量 isDone。因此,如果您选中第一个输入框,它将应用于每个后续任务。

您可以通过从事件对象中检索复选框的值来解决这个问题。 IE。 e.currentTarget.checked

另一种方法是为每个复选框使用唯一的 ID,并使用该 ID 检索值