首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

急 等 求改造一个简单的JS,该怎么解决

2012-06-05 
急等 求改造一个简单的JShtmlbodydiv idboxinput typebutton value+ onclickjavascript

急 等 求改造一个简单的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> 

热点排行
Bad Request.