In one template, I have:
在一个模板中,我有:
<template name="moviesTemplate">
<form>
<ul>
{{#each movies}}
<li>
{{title}} <input id="{{title}}" type="submit" value="Delete" />
</li>
{{/each}}
</ul>
</form>
</template>
When the user clicks Delete on the moviesTemplate, I want to find the id property of the input element from within the event in my javascript file:
当用户在moviesTemplate上单击Delete时,我想在我的javascript文件中的事件中找到input元素的id属性:
Template.moviesTemplate.events = {
'submit': function (e, tmpl) {
e.preventDefault();
var theId = theButtonThatRaisedTheEvent.id.toString(); // <--- This is what I'm referring to
}
}
How can I find this element? I thought it was 'e', but I can't seem to get any Id out of it. Perhaps I misunderstand...
我怎样才能找到这个元素?我以为它是'e',但我似乎无法从中得到任何ID。也许我误解了......
Edit 1:
编辑1:
Okay, so it seems that 'e' is the event, which doesn't contain any information related to the source element. How else do I go around accomplishing this? Do I need to rethink about how I'm doing this?
好的,所以似乎'e'是事件,它不包含任何与源元素相关的信息。我怎么回事呢?我是否需要重新思考我是如何做到这一点的?
4 个解决方案
#1
2
To me it sounds like the id belongs to the form, and not to the submit button. I would use the following:
对我来说,它听起来像id属于表单,而不是提交按钮。我会使用以下内容:
<template name="main">
<form data-id="anId">
<input type="submit" value="Delete!">
</form>
</template>
Template.main.events({
'submit form': function(event, template){
event.preventDefault() // Don't submit it!
var id = event.currentTarget.getAttribute('data-id') // Get the id attribute.
console.log(id)
}
})
Update
更新
Replace the template you have now with:
替换您现在拥有的模板:
<template name="moviesTemplate">
<ul>
{{#each movies}}
<li>
<form data-id="{{title}}">
{{title}} <input type="submit" value="Delete" />
</form>
</li>
{{/each}}
</ul>
</template>
And use the event handler previously written in this post.
并使用此帖子中先前编写的事件处理程序。
Actually, this
inside the event handler will be the context of the movie, so the handler can simply be:
实际上,事件处理程序中的这个将是电影的上下文,因此处理程序可以简单地为:
Template.main.events({
'submit form': function(event, template){
event.preventDefault() // Don't submit it!
var id = this.title // Get the title through the context.
console.log(id)
}
})
so there's no need using the data-id
attribute on the form.
所以不需要在表单上使用data-id属性。
#2
1
Unfortunately; it's not that straightforward.
不幸;这不是那么简单。
A starting point would be similar to this answer However you may want to handle things like "enter" key being pressed on element
一个起点类似于这个答案但是你可能想要处理诸如“输入”键被按下元素之类的东西
Template.moviesTemplate.events = {
'click input[type=submit]': function(e, tmpl){
tmpl.find('input[type=submit]').data('clicked',false);
$(e.currentTarget).data('clicked',true);
},
'submit': function (e, tmpl) {
e.preventDefault();
var theId = tmpl.find('input[type=submit]').filter(function(i,ele){
return $(ele).data('clicked');
}).get(0).attr('id');
}
}
#3
#4
0
You can create a single submit form event, and you conditionally check the event target field. Call appropriate Meteor method based on the collection you are inserting into.
您可以创建单个提交表单事件,并有条件地检查事件目标字段。根据要插入的集合调用适当的Meteor方法。
Template.detailsViewTemplate.events({
'submit form': function(ev){
ev.preventDefault();
var detail_input_field = template.$('#detail_entry');
var message_input_field = template.$('#message_entry');
if(detail_input_field != undefined){
var detailFormData = {
detail: $(ev.target).find('[name = detail]').val(),
parentId: $(ev.target).find('[name = parentId]').val(),
checkboxStatus: ''
}
Meteor.call('addDetail', detailFormData);
$('.form-group').children().val('');
} else if(message_input_field != undefined){
var newMessageData = {
listId: this.params_id,
message: $(ev.target).find('[name = message]').val()
}
Meteor.call('addMessage', newMessageData);
$('.form-group').children().val('');
}
}
}
#1
2
To me it sounds like the id belongs to the form, and not to the submit button. I would use the following:
对我来说,它听起来像id属于表单,而不是提交按钮。我会使用以下内容:
<template name="main">
<form data-id="anId">
<input type="submit" value="Delete!">
</form>
</template>
Template.main.events({
'submit form': function(event, template){
event.preventDefault() // Don't submit it!
var id = event.currentTarget.getAttribute('data-id') // Get the id attribute.
console.log(id)
}
})
Update
更新
Replace the template you have now with:
替换您现在拥有的模板:
<template name="moviesTemplate">
<ul>
{{#each movies}}
<li>
<form data-id="{{title}}">
{{title}} <input type="submit" value="Delete" />
</form>
</li>
{{/each}}
</ul>
</template>
And use the event handler previously written in this post.
并使用此帖子中先前编写的事件处理程序。
Actually, this
inside the event handler will be the context of the movie, so the handler can simply be:
实际上,事件处理程序中的这个将是电影的上下文,因此处理程序可以简单地为:
Template.main.events({
'submit form': function(event, template){
event.preventDefault() // Don't submit it!
var id = this.title // Get the title through the context.
console.log(id)
}
})
so there's no need using the data-id
attribute on the form.
所以不需要在表单上使用data-id属性。
#2
1
Unfortunately; it's not that straightforward.
不幸;这不是那么简单。
A starting point would be similar to this answer However you may want to handle things like "enter" key being pressed on element
一个起点类似于这个答案但是你可能想要处理诸如“输入”键被按下元素之类的东西
Template.moviesTemplate.events = {
'click input[type=submit]': function(e, tmpl){
tmpl.find('input[type=submit]').data('clicked',false);
$(e.currentTarget).data('clicked',true);
},
'submit': function (e, tmpl) {
e.preventDefault();
var theId = tmpl.find('input[type=submit]').filter(function(i,ele){
return $(ele).data('clicked');
}).get(0).attr('id');
}
}
#3
0
Use tmpl.find
and grab id
:
使用tmpl.find并获取id:
Template.moviesTemplate.events = {
'submit':function(e, tmpl){
e.preventDefault();
},
'click input[type="submit"]':function(e, tmpl){
console.log(e.currentTarget.id)
}
}
证明
#4
0
You can create a single submit form event, and you conditionally check the event target field. Call appropriate Meteor method based on the collection you are inserting into.
您可以创建单个提交表单事件,并有条件地检查事件目标字段。根据要插入的集合调用适当的Meteor方法。
Template.detailsViewTemplate.events({
'submit form': function(ev){
ev.preventDefault();
var detail_input_field = template.$('#detail_entry');
var message_input_field = template.$('#message_entry');
if(detail_input_field != undefined){
var detailFormData = {
detail: $(ev.target).find('[name = detail]').val(),
parentId: $(ev.target).find('[name = parentId]').val(),
checkboxStatus: ''
}
Meteor.call('addDetail', detailFormData);
$('.form-group').children().val('');
} else if(message_input_field != undefined){
var newMessageData = {
listId: this.params_id,
message: $(ev.target).find('[name = message]').val()
}
Meteor.call('addMessage', newMessageData);
$('.form-group').children().val('');
}
}
}