我正在开发一个简单的应用程序来添加和删除 ul 中的名称。我有一个输入和一个按钮,当我单击按钮时,输入中的文本会附加到 ul。我的代码是:
<input type="text" placeholder="Enter friend's name" id="input" />
<button id="add-input">Add Friend</button>
<ul id="friends-list">
</ul>
骨干代码:
<script>
$(function() {
FriendList = Backbone.Collection.extend({
initialize: function(){
this.bind("add", function( model,options ){
var id = ( model.collection.indexOf(model) );
view.render(model,id);
});
this.bind("remove",function(model){
alert("here");
});
}
});
FriendView = Backbone.View.extend({
tagName: 'li',
events: {
'click #add-input': 'getFriend',
'click .button': 'removeFriend'
},
initialize: function() {
this.friendslist = new FriendList;
_.bindAll(this, 'render');
},
getFriend: function() {
var friend_name = $('#input').val();
this.friendslist.add( {name: friend_name} );
},
removeFriend: function(){
var friend_index = $('.button').attr('id');
alert(friend_index);
this.friendslist.remove();
},
render: function( model,id ) {
$("#friends-list").append("<li>"+ model.get("name")+"<button class=button id="+id+">"+"delete"+"</button>"+"</li>");
$('#input').val('');
}
});
var view = new FriendView({el: 'body'});
});
</script>
我的问题,我卡在哪里:
i) 添加功能运行良好,当我单击删除按钮时,它会转到 removeFriend 功能,但不会转到集合和警报(“此处”);ii) 请帮我写代码来删除/删除点击删除按钮的 li
谢谢