背景:
我正在编写一份注册表,用户可以在其中输入有关无限数量人的信息。每个人都作为json对象输入,由字段组成,如下所示。
addPerson: function() {
//Create a json object for this person.
var person = {
id: $("#dialog").data("person"),
fname: $("#dialog input[name=pfname]").attr("value"),
lname: $("#dialog input[name=plname]").attr("value"),
title: $("#dialog input[name=ptitle]").attr("value"),
bio: $("#dialog textarea[name=pbio]").attr("value"),
photo: $("#dialog input[name=photo]").attr("value"),
owner: $("#dialog input[name=owner]").prop("checked") ? $("input[name=owner]").attr('checked', true),
percent: $("#dialog input[name=ppercent]").attr("value"),
edu: $("#dialog textarea[name=pedu]").attr("value"),
skills: $("#dialog textarea[name=pskills]").attr("value"),
prof: $("#dialog textarea[name=pprof]").attr("value"),
awards: $("#dialog textarea[name=pawards]").attr("value"),
community: $("#dialog textarea[name=pcommunity]").attr("value"),
years: $("#dialog input[name=pyears]").attr("value"),
compensation: $("#dialog textarea[name=pcompensation]").attr("value"),
}
$(this).dialog("close");
upsmart.people.finishAddPerson(person);
},
添加对象后,每个人都会在网格上显示其个人资料图片和名称,请参阅以下代码:
finishAddPerson: function(person) {
upsmart.people.people[person.id] = person;
if($("#person"+person.id).length == 0) {
box = $("<div class='person'></div>").attr("id","person"+person.id);
box.data("person",person.id);
box.insertBefore($("#new"));
} else {
box = $("#person"+person.id);
box.html("");
}
box.append($("<img/>").attr("src",person.photo));
box.append($("<div/>").attr("class","label").html(person.fname+" "+person.lname));
}
我在哪里: 根据benashby的this answer,我修改了finishAddPerson函数,如下所示:
finishAddPerson: function(person) {
upsmart.people.people[person.id] = person;
if($("#person"+person.id).length == 0) {
box = $("<div class='person'></div>").attr("id","person"+person.id);
box.data("person",person.id);
box.insertBefore($("#new"));
} else {
box = $("#person"+person.id);
box.html("");
}
box.append($("<img/>").attr("src",person.photo));
//added a delete button here
box.append($("<button/>").attr("class","remove-user-btn"));
box.append($("<div/>").attr("class","label").html(person.fname+" "+person.lname));
//delete function added here, using person.id to signify the profile to be removed
$('.remove-user-btn').bind('click', function(e) {
removePerson($(this).attr(person.id);
//Remove the containing div here as well
$(this).parent().remove()
});
}
我的问题:
单击它时删除按钮失败,并在我的控制台中出现以下错误:
ReferenceError:未定义removePerson
removePerson($(本).attr(person.id));
我想我应该发布免责声明,我仍然通过教自己的javascript和jquery,所以如果问题看起来很简单......它可能是。不过,对此事的任何帮助都将不胜感激。提前谢谢了! : - )