背景:
我正在制作一个注册表单,用户可以在其中输入有关无限人数的信息。每个人都作为一个 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 的这个答案,我修改了 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($(this).attr(person.id));
我想我应该发出免责声明,我仍然在自学javascript和jquery,所以如果问题看起来真的很简单......它可能是。尽管如此,对于此事的任何帮助将不胜感激。提前谢谢了!:-)