我正在构建一个jQuery可排序列表,其中用户可以从表中添加项目,拖动和排序和/或删除他们。我可以添加和排序没有问题,但我不能解决如何删除项目元素添加后。我对js/jQuery相对来说比较陌生,所以我有一种感觉,在这里学到一些关于它如何工作的新东西!jQuery创建后删除元素
我会离开这里了ui.sortable的东西,我只关心删除项目..
<table>
<tr>
<td><a class="addrelease" href="#" cat_id="1">add</a></td>
<td>Item 1</td>
</tr>
<tr>
<td><a class="addrelease" href="#" cat_id="2">add</a></td>
<td>Item 2</td>
</tr>
</table>
<div id="list"></div>
<script>
$("a.addrelease").click(function (e) {
e.preventDefault();
cat_id = $(this).attr('cat_id');
remove_str = " <a href=\"#\" class=\"remove\">remove</a>";
str = cat_id + remove_str;
$(str).appendTo("#list").hide().fadeIn();
});
$("a.remove").click(function (e) {
alert("This function doesn't seem to be called");
$(this).parent().remove(); //Doesn't happen..
});
</script>
我猜,JavaScript不会把所生成的新项目 - 但我不敢肯定,所以我不知道从哪里开始修复它
干杯
啊太棒了!这就是诀窍!哇,我不知道要多久才能找到解决方案!由于 – Alex 2011-03-23 03:39:30
我想你也应该更换'$(本).parent()删除();''由$(这)一个.remove();'否则整个'div'被删除。 – 2011-03-23 03:40:39
哦,是的,你说得对 - 我忘了编辑位。生成的元素应该是这样的,而不是:''