日期:2014-05-16 浏览次数:20447 次
<html>
<body>
<div id="box">
<input type="button" value="+" onclick="javascript:addRemoveItem('add','box');" /><br />
<input type="button" value="" id="record"/><br/>
</div>
<script type="text/javascript">
var maxid = 0;
function addRemoveItem(act,target){
var record = document.getElementById("record");
if ((!act || act == 'add') && target){
var $box = typeof(target) != 'object' ? document.getElementById(target) : target;
if (!$box) return;
var obj = document.createElement('<SPAN>');
var id = "input_" +maxid + "_id";
obj.innerHTML = maxid + " <input type='text' name='input_"+maxid+"' value='' />" + '<input type="button" value="-" onclick="javas'+'cript:addRemoveItem(\'del\',this.parentNode);" /><br />';
$box.appendChild(obj);
obj = $box = null;
record.value += (maxid == 0?"":",")+id;
maxid++;
} else if (act == 'del' && target){
if (typeof(target) != 'object') return;
target.parentNode.removeChild(target);
var idno = window.parseInt(target.childNodes[1].name.split("_")[1]);
record.value = record.value.replace((idno == 0?"":",")+"input_"+idno+"_id","");
maxid--;
}
}
</script>
</body>
</html>