使用js动态添加文本框 并在后台取文本框数据

本文介绍了一种使用JavaScript动态生成表格的方法,并演示了如何通过按钮点击事件添加新行到表格中,同时收集这些行的数据并存储在隐藏字段内,以便于后续处理。

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

js部分
 <script language="javascript" type="text/javascript">

var count=0 ;
function additem(id)
{
  var row,cell,str;
  row = document.getElementById(id).insertRow(count+1);
  if(row != null )
  {

row.insertCell(0).innerHTML="";//"<td align=\"right\": style=\"border-left-style: none; border-left-width: medium; border-top-style: none; border-top-width: medium;\">处罚依据:</td> ";
row.insertCell(1).innerHTML="<td style=\"border-left-style: none; border-left-width: medium; border-top-style: none; border-top-width: medium;\" colspan=\"3\"><input style=\"width:420px\" id=\"St"+count+"\" type=\"text\" name=\"St"+count+"\"> </td>";//<input type=\"button\" class=\"button\" value=\"删除\" οnclick=\'deleteitem(this);\'>
count ++;
  }
}
//function deleteitem(obj)
//{
//  var curRow = obj.parentNode.parentNode;
//  tb.deleteRow(curRow.rowIndex);
//}

function getsub()
{
var re="";
for (var  i = 0 ;i<count;i++)
{
re += document.getElementsByName("St"+i)[0].value+"</br>";

}
document.getElementById("Hidvalue").value=re;
//document.getElementById("Hidhtml").value = document.getElementById("tb").innerHTML;
}


    </script>

动态生成位置的table

  <table id="tb" border="1" width="100%" bordercolor="#000033" >
            <tr>
                <td align="right" width="120px" style=" border:none;">
                    处罚依据:</td>
                <td colspan="3" style=" border-left-style: none; border-left-width: medium; border-top-style: none; border-top-width: medium;">
                    <asp:TextBox ID="Txt_cfyj" runat="server" MaxLength="50" Width="65%"></asp:TextBox>
                    <a style="color: Red;">*</a><asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="Txt_cfyj" ErrorMessage=""></asp:RequiredFieldValidator>
               
                    <input class="button" name="button" οnclick='additem("tb")' type="button" value="添加" />
                </td>
            </tr>
        </table>     
使用的部分:


string aa = Request.Form["Hidvalue"];
         


转载于:https://www.cnblogs.com/huangbaoyu/archive/2008/02/22/1077567.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值