我有如下所示的backbone.js 视图。它工作正常,现在单击表单按钮时单击事件不会执行。控制台中没有错误,也没有显示任何其他明显的原因迹象。
window.FormOptionView = Backbone.View.extend({
template: _.template($('#test-option').html()),
render: function (eventName) {
$('#test1').append(this.template(this.model.toJSON()));
$('#test2').append(this.template(this.model.toJSON()));
return this;
}
});
window.AddTestView = Backbone.View.extend({
template: _.template($('#test-add').html()),
initialize: function () {
this.collection.bind("reset", this.render, this);
this.render();
},
events: {
"click button": "addTest"
},
addTest: function(event){
event.preventDefault();
var test = new Test({
test1: {
firstName: $("#test1 option:selected").val(),
lastName: $("#test1Score").val()
},
test2: {
firstName: $("#test2 option:selected").val(),
lastName: $("#test2Score").val()
}
});
test.add(result);
app.navigate("tests", true);
},
render: function (eventName) {
$('#content').html(this.template());
_.each(this.collection.models, function (test) {
this.renderSelectBox(test);
}, this);
return this;
},
renderSelectBox: function (item) {
var optionView = new FormOptionView({
model: item
});
$(this.el).append(optionView.render().el);
}
});
对应的 HTML
<script type="text/template" id="test-option">
<option><%= firstName %> <%= lastName %></option>
</script>
<script type="text/template" id="test-add">
<form id="addTest" action="#">
<label for="test1">Test1:</label>
<select id="test1">
<option></option>
</select>
<label for="test1Score">Score:</label>
<input id="test1Score" type="number" />
<label for="test2">Test 2:</label>
<select id="test2">
<option></option>
</select>
<label for="test2Score">Score:</label>
<input id="test2Score" type="number" />
<button id="add">Add</button>
</form>
</script>