为什么我的视图事件没有触发?
这是我的 CoffeeScript 代码:
$(document).ready ->
SearchView = Backbone.View.extend
tagName: "form"
className: "search"
events: {
"click label":"search"
}
search: ->
console.log("HERE")
searchView = new SearchView()
这是我的 HTML:
%form#search.search
%label
Search
%input
但单击表单不会触发任何事件。有什么想法吗?
Here's my CoffeeScript code:
$(document).ready ->
SearchView = Backbone.View.extend
tagName: "form"
className: "search"
events: {
"click label":"search"
}
search: ->
console.log("HERE")
searchView = new SearchView()
And here's my HTML:
%form#search.search
%label
Search
%input
But clicking the form is not triggering any events. Any ideas why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您误解了
tagName
和className
的工作原理。当您设置这些时:这仅意味着您将拥有
如果您想使用
tagName
和className
那么您将需要render
来充实您的表单:然后您将调用
render
并将el
添加到 DOM,如下所示:演示:http://jsfiddle.net/ambigously/ubqqE/
如果你想绑定到现有的 HTML,那么你可以使用
el
而不是tagName 和
className
像这样:演示:http://jsfiddle.net/ambiguously/FfDg6/
You misunderstand how
tagName
andclassName
work. When you set these:That just means that you'll have
<form class="search"></form>
in@el
. Those settings don't bind your view to what theform.search
selector matches and you never add@el
to the DOM so your events don't end up bound to anything at all.If you want to use
tagName
andclassName
then you'll want arender
to flesh out your form:And then you'd call
render
and addel
to the DOM with something like this:Demo: http://jsfiddle.net/ambiguous/ubqqE/
If you wanted to bind to existing HTML then you could use
el
instead oftagName
andclassName
like this:Demo: http://jsfiddle.net/ambiguous/FfDg6/