我有以下javascript代码,当用户需要添加更多行时,它会复制容器:
<script type="text/javascript">
//<![CDATA[
document.getElementById('button').onclick = duplicate;
var i = 0;
var original = document.getElementById('inlinedup');
function duplicate() {
var clone = original.cloneNode(true); // "deep" clone
clone.id = "inlinedup" + ++i; // there can only be one element with an ID
original.parentNode.insertBefore(clone, original.nextSibling);
}
//]]>
</script>
我的问题是,如何编辑这个javascript,以便在每次重复时也将名称数组增加一个?
示例:
name="prepmethod[$i]"
请看小提琴的工作例子。
http://jsfiddle.net/79WYA/8/
呃。。。你不需要。
如果您有:
<input type="text" name="prepmethod[]" value="1" />
<input type="text" name="prepmethod[]" value="2" />
<input type="text" name="prepmethod[]" value="3" />
<input type="text" name="prepmethod[]" value="4" />
然后,服务器上得到的数组将是array(1,2,3,4)
。就是这样,你什么都不用做。
顺便说一句,我建议只设置clone.id = "";
,除非您以后有特殊需要使用ID。