event.target jquery:语法上的差异
我正在学习David Turnbull的第二个Meteor应用程序(待办事项列表应用程序)教程,类似的事件使用了不同的jquery语法,我不太明白为什么 第一部分涉及添加任务的功能: html: 第二部分涉及使用keyup功能编辑任务: html: 我试图在第二部分(编辑任务)中使用下面的event.target查询,但它不起作用:event.target jquery:语法上的差异,jquery,meteor,Jquery,Meteor,我正在学习David Turnbull的第二个Meteor应用程序(待办事项列表应用程序)教程,类似的事件使用了不同的jquery语法,我不太明白为什么 第一部分涉及添加任务的功能: html: 第二部分涉及使用keyup功能编辑任务: html: 我试图在第二部分(编辑任务)中使用下面的event.target查询,但它不起作用: var todoItem = $('[name="todoItem"]').val(); 为什么呢?在什么情况下我们使用哪个?您有多个ToDoItem,它们都有相
var todoItem = $('[name="todoItem"]').val();
为什么呢?在什么情况下我们使用哪个?您有多个ToDoItem,它们都有相同的名称。相反,在不使用jquery的情况下尝试
event.target.inputName.value
//event
Template.todoItem.events({
'keyup [name=todoItem]': function(event){
var documentId = this._id;
var todoItem = event.target.todoItem.value;
Todos.update({ _id: documentId }, {$set: {name: todoItem }});
}
});
如果它不起作用,可能是因为您的输入没有唯一的id/类。如果您用
(event.target)替换
,它应该可以工作,但不要认为这是最好的解决方案。我可能会将每个任务的id添加到
中,比如id=“{{{u id}}”
并使用该id选择元素。您有多个ToDoItem,它们都有相同的名称。相反,在不使用jquery的情况下尝试event.target.inputName.value
//event
Template.todoItem.events({
'keyup [name=todoItem]': function(event){
var documentId = this._id;
var todoItem = event.target.todoItem.value;
Todos.update({ _id: documentId }, {$set: {name: todoItem }});
}
});
如果它不起作用,可能是因为您的输入没有唯一的id/类。如果您用
(event.target)替换
,它应该可以工作,但不要认为这是最好的解决方案。我可能会将每个任务的id添加到
中,比如id=“{{{u id}}”
并使用该id选择元素
Template.todoItem.events({
'keyup [name=todoItem]': function(event){
var documentId = this._id;
var todoItem = $(event.target).val();
Todos.update({ _id: documentId }, {$set: {name: todoItem }});
}
});
var todoItem = $('[name="todoItem"]').val();
//event
Template.todoItem.events({
'keyup [name=todoItem]': function(event){
var documentId = this._id;
var todoItem = event.target.todoItem.value;
Todos.update({ _id: documentId }, {$set: {name: todoItem }});
}
});