【发布时间】:2015-06-22 19:29:10
【问题描述】:
我的 html 设置是这样的。
<div id="product-module">
<ul id="product">
<li><input name="product" type="radio"/></li>
<li><input name="product" type="radio"/></li>
<li><input name="product" type="radio"/></li>
</ul>
<table id="quantities"/>
<script id="myTemplate" type="text/x-handlebars-template">
{{#each this}}
<tr>
<td class="quantity">
<label class="checked" for="quantity_{{id}}">
<input type="radio" value="" name="quantity">
{{quantity}}
</label>
</td>
</tr>
{{/each}}
</script>
</div>
我正在尝试设置一个事件处理程序来侦听对 li 的点击并在数量元素中呈现模板。
我的主干观点
el: '#product-module',
template: Handlebars.compile($("#myTemplate").html()),
events: {
"click #product li": "liClicked",
},
initialize: function(){
this.listenTo(this.collection, 'reset', this.render);
},
render: function() {
console.log('model ' + this.template(this.collection.toJSON()))
this.$el.html( this.template(this.collection.toJSON()));
},
liClicked: function(event, callback){
console.log('liClicked')
},
如果我将 el 更改为 #quantities,那么我的事件处理程序将无法在 el 之外工作。如果我将我的 el 更改为 #product-module,那么一切都会被替换。如何让我的事件处理程序监听并在 #quantities 元素中呈现模板?
我也试过了,没有成功
$('#quantities', this.el)
TypeError: $(...) 不是函数
【问题讨论】:
-
你想用
$('#quantities', this.el)做什么?如果您尝试将this.el插入#quantities,那么您需要$('#quantities').append(this.el); -
我不确定是否有一种“骨干”方式来处理这个问题。另一个解决方案似乎是使用 jQuery,这要求我必须将我的 js 包装在一个 jQuery 函数中。
-
If i change my el to #product-module, then everything gets replaced.被替换的 HTML 应该是模板的一部分。有什么理由不可以吗? -
#myTemplate模板是什么样的? -
我在模板之外有一个包含单选按钮的列表。我不想刷新收音机,我想刷新下面的模板。
标签: javascript jquery backbone.js