用于删除动态项目的功能

时间:2014-03-04 11:01:43

标签: javascript function field items

我有这样的剧本。如何继续添加功能:删除项目?

JS

counter = 0;
function action() {
  counterNext = counter + 1;
  document.getElementById("input"+counter).innerHTML = "<p><input type='text' name='data[]' class='name'>&nbsp;&nbsp;<a href='#' class='remove'><img src='remove.png' width='20' height='20' border='0' id='remove'/></a></p><div id=\"input"+counterNext+"\"></div>";
  counter++;
}

HTML

<p>
  <input type='text' name='data[]' class='name'>
  <a href="javascript:action();">Add</a>
</p>
<div id="input0"></div>

有人可以帮助我......

1 个答案:

答案 0 :(得分:0)

要删除项目,请更改以下代码:

<a href='#' class='remove'>

为:

<a href='#' onclick='this.parentElement.parentElement.removeChild(this.parentElement); return false;' class='remove'>

示例:http://jsfiddle.net/uRLFR/

相关问题