我有一个 Document
集合,其中包含保存在输入
上的表单:
document_form.html:
<template name="documentForm">
<form class="form" role="form">
<input name="title" type="text" value="{{title}}">
</form>
</template>
document_form.js:
Template.documentForm.events({
"input input": function(e) {
e.preventDefault();
currentDocumentId = this._id;
setTimeout(function() {
Documents.update(currentDocumentId, {
$set: {
title: $(e.target).find('[name=title]').val()
}
});
}, 3000);
}
});
如您所见,Document
文档应在输入后三秒更新。
我看到的是新输入三秒钟,然后旧输入返回(旧输入正在恢复)。
所以,比如说,我有一个名为 Untitled
的文档
。我将输入 AnotherTitle
,然后 3 秒后输入将恢复为 Untitled
。
我在这里做错了什么?
最佳答案
我认为问题是你不应该使用find('[name=title]')
。
find()
用于获取元素的后代。在这里,您的目标是输入字段,因此使用以下内容就足够了:
标题:$(e.currentTarget).val()
希望您觉得它有用!
关于javascript - 为什么 Meteor 不更新输入事件的以下输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28124985/