这看起来很简单,但我不能让它工作。我可以添加DOM元素,但在使用数组时无法删除它们。Javascript removeChild array
<script language="javascript">
fields = 0;
count = 0;
function addInput() {
if (fields != 10) {
var htmlText = "<input type='search' value='' name='field[]' />";
var remButton = "<input type='button' value='del' onclick='remove()' />";
var newElement = document.createElement('div');
newElement.id = 'SomeID'+fields;
newElement.innerHTML = htmlText + remButton + newElement.id;
var fieldsArea = document.getElementById('text');
fieldsArea.appendChild(newElement);
fields += 1;
} else {
...
}
count++;
}
// NEED HELP FROM HERE PLEASE
// You don't need to Loop, just get the button's id and remove that entire 'SomeID'
function remove() {
fieldsArea = document.getElementById('text');
fieldsArea.removeChild(SomeID1); <-----------------------THIS WORKS!
fieldsArea.removeChild(SomeID+count); <------------------THIS JUST WOULDN'T
count--;
}
</script>
在删除功能,书写SomeID1工程和删除第一个添加的元素,但是当我尝试使用“计数”,我只是不能删除我的“元素”。
任何帮助将不胜感激。
谢谢!
谢谢菲利克斯·克林 - 我正在尝试它:) – lixcab 2011-03-14 22:35:30
@lixcab:你的HTML中有一些错误(如重复关闭的body标签)。我纠正了你的小提琴,并使删除按钮的工作。看看它:http://jsfiddle.net/fkling/GnwpM/1/你也不需要'count'变量。希望有所帮助! – 2011-03-14 22:45:26
再次感谢!现在审查!:) – lixcab 2011-03-14 22:47:33