js實現(xiàn)動態(tài)增加文件域表單功能

本文實例為大家分享了js實現(xiàn)動態(tài)增加文件域表單的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)新互聯(lián)專注于明山企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站建設(shè)。明山網(wǎng)站建設(shè)公司,為明山等地區(qū)提供建站服務(wù)。全流程按需求定制開發(fā),專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

js實現(xiàn)動態(tài)增加文件域表單功能

實現(xiàn)代碼:

<html>
<head>
 <title>動態(tài)添加表單元素</title>
</head>

<script language="javascript"> 
 //全局變量,代表文件域的個數(shù),并用該變量區(qū)分文件域的name屬性 
 var file_count = 0;
 //增加文件 域 
 function additem(id) {
  if (file_count > 9) {
   alert("最多10個 ");
   return;
  }
  //定義行變量row;單元格變量cell;單元格內(nèi)容變量str。 
  var row, cell, str;
  //在指定id的table中插入一行 
  row = eval("document.all[" + '"' + id + '"' + "]").insertRow();
  if (row != null) {
   //設(shè)置行的背景顏色 
   row.bgColor = "white";
   //在行中插入單元格 
   cell = row.insertCell();
   //設(shè)置str的值,包括一個文件域和一個刪除按鈕 
   str = '<input onselectstart="return false" class="tf" onpaste="return false" type="file" name="file[' + file_count + ']"  onkeydown="return false;"/>';
   str += " <input type=" + '"' + "button" + '"' + " value=" + '"' + "刪除" + '"' + " onclick='deleteitem(this," + '"' + "tb" + '"' + ");'>";
   //文件域個數(shù)增加 
   file_count++;
   //設(shè)置單元格的innerHTML為str的內(nèi)容 
   cell.innerHTML = str;
  }
 }
 //刪除文件域 
 function deleteitem(obj, id) {
  var rowNum, curRow;
  curRow = obj.parentNode.parentNode;
  rowNum = eval("document.all." + id).rows.length - 1;
  eval("document.all[" + '"' + id + '"' + "]").deleteRow(curRow.rowIndex);
  file_count--;
 } 
</script>

<body>
 <input type=button value="增加" onclick='additem("tb")' /><br/>
 <table cellspacing="0" id="tb" >
 </table>

</html>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當前名稱:js實現(xiàn)動態(tài)增加文件域表單功能
URL網(wǎng)址:http://bm7419.com/article36/pcoepg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作網(wǎng)站收錄、品牌網(wǎng)站制作、Google、自適應(yīng)網(wǎng)站、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化