日期:2014-05-16  浏览次数:20334 次

急 等 求改造一个简单的JS
<html>
<body>
<div id="box">
<input type="button" value="+" onclick="javascript:addRemoveItem('add','box');" /><br />
<input type="button" value="" id="record"/>
</div>
<script type="text/javascript">
var maxid = 0;
function addRemoveItem(act,target){
  if ((!act || act == 'add') && target){
  var $box = typeof(target) != 'object' ? document.getElementById(target) : target;
  if (!$box) return;
  var obj = document.createElement('<SPAN>');
  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;
  maxid++;
  } else if (act == 'del' && target){
  if (typeof(target) != 'object') return;
  target.parentNode.removeChild(target);
  maxid--;
  }
}
</script>
</body>
</html>


现在我想把所有添加的新的input的id 记录在一个id为 record的 input里,并且删除掉的建立的input的ID在 record中也会被删除。。。求高手帮忙改造。。。

------解决方案--------------------
HTML code
<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>