动态添加删除表单

本文介绍了如何使用C#读取指定文件夹内的.jpg图片,并展示了一种通过JavaScript动态克隆DOM元素的方法来增加或删除表单输入项,适用于表单需要动态增删字段的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 读取文件夹中某类型的图片

using System.IO;

        DirectoryInfo imagesfile = new DirectoryInfo (Server.MapPath("~/image" ));

        FileInfo [] fileinfo = imagesfile.GetFiles("*.jpg" );

        foreach (FileInfo fi in fileinfo)

        {

            strpic += "<img src=/"../image/" + fi.Name + "/" />" ;

        }

 

 

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function $(obj)
{
    return document.getElementById(obj);
}
function cloneObj(oClone, oParent, count)
{
   if(oParent.childNodes.length < count)
   {
      var newNode = oClone.cloneNode(true);  //复制表单
      oParent.appendChild(newNode);
      return newNode;
   }
   return false;
}

function delObj(oParent, count)
{
   if(oParent.childNodes.length > count)
   {
      oParent.removeChild(oParent.lastChild);  //删除最后一个
      return true;
   }
   return false;
}

function clonePoll(maxpoll, arrayinput, arraydiv)
{
   for(c=0;c<arrayinput.length;c++)
   {
      var newNode = cloneObj($(arrayinput[c]), $(arraydiv[c]) , parseInt(maxpoll) + 1);
      if(!newNode)
      {
         alert("时间项不能多于 " + maxpoll + " 个");
      }
      var inputs = newNode.getElementsByTagName("input");
      for(i in inputs)
      {
         inputs[i].value = "";
         if(inputs[i].name == arraydiv[c])
         {
            inputs[i].id = arraydiv[c];
         }
      }
   }
}
</script>
</head>

<body>
<div id="polloptions">
     <div id="divPollItem" style="float: left;">
          <input type="text" size="30" id="pollitemid" name="pollitemid" maxlength="30" onfocus=" " />
</div>
</div>

<div id="polloptions2" style="float: left;">
     <div id="divPollItem2">
          <input type="text" size="30" id="pollitemid2" name="pollitemid2" maxlength="30" onfocus=" " />
          <input type="file" />
</div>
</div>
<input name="button" type="button" onclick="clonePoll('20',['divPollItem','divPollItem2'],['polloptions','polloptions2'])" value="增加时间项" />
<input name="button" onclick="if(!delObj(document.getElementById('polloptions'),1) || !delObj(document.getElementById('polloptions2'),1)){alert('时间项不能少于1个');}" type="button" value="删除时间项" />
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值