Javascript實現自動添加控制項

來源:互聯網
上載者:User

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<body>
<script   type="text/javascript">
var   count   =   0;
function   Add(num){
      count+= 1;

      var   File1   =   document.getElementById("file1");
      var   div   =   document.createElement("div");
      var   countTxt   =   document.createTextNode("文字框"+count);
      var   inputTxt   =   document.createElement("input");
      inputTxt.type   =   "text";
      inputTxt.value   =   document.getElementById(num).value;
      inputTxt.name   =   "txt"+count;
      var   inputTxta   =   document.createElement("input");
      inputTxta.type   =   "text";
      inputTxta.value   =   document.getElementById("a"+num).value;
      inputTxta.name   =   "txt"+count;
      var   inputTxtaa   =   document.createElement("input");
      inputTxtaa.type   =   "text";
      inputTxtaa.value   =   document.getElementById("aa"+num).value;
      inputTxtaa.name   =   "txt"+count;
      var   inputTxtaaa   =   document.createElement("input");
      inputTxtaaa.type   =   "text";
      inputTxtaaa.value   =   document.getElementById("aaa"+num).value;
      inputTxtaaa.name   =   "txt"+count;
      var   inputTxtaaaa   =   document.createElement("input");
      inputTxtaaaa.type   =   "text";
      inputTxtaaaa.value   =   document.getElementById("aaaa"+num).value;
      inputTxtaaaa.name   =   "txt"+count;
      var   btn   =   document.createElement("input");
      btn.type   =   "button";
      btn.value   =   "刪除";
      btn.onclick=function()
      {
        this.parentNode.parentNode.removeChild(this.parentNode);

        var n = File1.getElementsByTagName("div");
        for(var k=0; k<n.length; k++)
        {
            n[k].firstChild.nodeValue = "文字框"+(k+1);
        }
        count -= 1;
      }

      div.appendChild(countTxt);
      div.appendChild(inputTxt);
      div.appendChild(inputTxta);
      div.appendChild(inputTxtaa);
      div.appendChild(inputTxtaaa);
      div.appendChild(inputTxtaaaa);
      div.appendChild(btn);
      File1.appendChild(div);
}
</script>
<div   id="file1"> </div>

<input   name="xiyaoname"   id="1"   value="Add"   type="button"   onclick="Add(1);"   />
<input   name="hiddenField"   type="hidden"   id="a1"   value="sex"/>
<input   name="hiddenField"   type="hidden"   id="aa1"   value=" birthday" />
<input   name="hiddenField"   type="hidden"   id="aaa1"   value="shenggao"   />
<input   name="hiddenField"   type="hidden"   id="aaaa1"   value="tizhong"   />

</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.