posts - 22, comments - 17, trackbacks - 0, articles - 15
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

怎样动态添加文本框并处理

Posted on 2007-01-04 14:44 码农cz 阅读(1424) 评论(1)  编辑  收藏 所属分类: As u

我在做项目时曾碰到这个问题,怎样动态地添加一个文本框或文件域,这在网上很容易找到。但对怎样处理提交的动态数据就很少有这方面的信息了。本人翻阅了一些资料,把自己的方法写了下来,以飨读者。

1.提交表单

 

<html>
<body>
 <form name
= " form "  method = " post "  action = " add.jsp " >
   <input name
= " button "  type = button onClick = 'additem( " tb " )' value = " 添加>> " >
 <table id
= " tb " >
 </table>
<script language
= " javascript " >

function additem(id)
{
  var row
, cell , str ;
  row  =  eval( " document.all[ " +' " '+id+' " '+ " ] " ).insertRow() ;
  if(row ! =  null )
     {
        cell 
=  row.insertCell() ;
        str = " <input type= " +' " '+ " text " +' " '+ "  name= " +' " '+ " StuName " +' " '+ " ><input type= " +' " '+ " button " +' " '+ "  value= " +' " '+ " 删除 " +' " '+ "  onclick='deleteitem(this, " +' " '+ " tb " +' " '+ " );'> "
   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) ;
}
</script>
   <p>
     <input type
= " submit "  name = " submit "  value = " 提交 " >
   </p>
 </form>
</body>
</html>

 

2.处理数据

< body >
< table  border ="1" >< tr >< td  colspan ="2" > eg </ td ></ tr >
<%
Enumeration params
= request.getParameterNames();
while (params.hasMoreElements()){
String  name = ( String )params.nextElement();
String [] values = request.getParameterValues(name);
if (name.equals( " StuName " )){
%>
< tr >
< td > <% = name %> </ td >
< td >
<%
for ( int  index = 0 ;index < values.length;index ++ ){
%>
<% = values[index] %>
<% }} %>
</ td >
</ tr >
</ table >
</ body >


评论

# re: 怎样动态添加文本框并处理  回复  更多评论   

2007-10-26 14:46 by 123
good ,thx!

只有注册用户登录后才能发表评论。


网站导航: