圖例:
按新增作者後
<script>
function addinput(){
var obj_0 = document.getElementsByName("addmember");
var items="";
//保存原有值
for(i=0; i<obj_0.length ; i++){
if(items==""){
items = document.getElementsByName("addmember")[i].value;
}else{
items += ","+ document.getElementsByName("addmember")[i].value;
}
}
if( obj_0.length==0 ){
//第一次新增
var obj = document.getElementById("content");
obj.innerHTML = obj.innerHTML + "<br>作者:<input type='text' name='addmember' value='' />";
}else{
//第二次以後
//先清除原有input 項目,再重新新增
document.getElementById("content").innerHTML="";
var obj = document.getElementById("content");
if( items.indexOf(",")>0 ){
var ss =items.split(",");
for(i=0; i<ss.length ; i++){
var val = ss[i];
obj.innerHTML += "<br>作者:<input type='text' name='addmember' value='"+ val + "' />";
}
}else{
obj.innerHTML = "<br>作者:<input type='text' name='addmember' value='"+ items + "' />";
}
obj.innerHTML += "<br>作者:<input type='text' name='addmember' value='' />";
}
}
<form name="myForm">
<div id="myDiv" style="display:none">test</div><br/>
作者:<input type="text" name="member" /><input type='button' id="add" value="新增作者" onclick="addinput();" /><br/>
<span id="content"></span><br/>
</form>
</body>