js实现动态生成表格中,并且表格中可以输入

JavaScript013

js实现动态生成表格中,并且表格中可以输入,第1张

<html xmlns=" http://www.w3.org/1999/xhtml" > <head> <title>标题页</title> <script type="text/jscript">

document.write ("<table border='1'>")

for(i=0i<5i++){

document.write ("<tr>")

document.write ("<td>")

document.write ("<input type='text' value=第"+i+"行><br>")//动态输出文本框

document.write ("</td>")

document.write ("</tr>")

}

document.write ("</table>")

</script> </head>

<body></body>

</htmml>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>MyHtml.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="this is my page">

    <meta http-equiv="content-type" content="text/html charset=UTF-8">

<script>

function addtextarea(x){

 if(x==""){

  count=0

 }else{

  count=x

 }

 var inputs = document.getElementById("inputs")

    while(inputs.hasChildNodes()) //每次动态添加应该更新内容,当div下还存在子节点时 循环继续

     {

     inputs.removeChild(inputs.firstChild)

     }

 for (var i=0i<counti++){ 

  var username = document.createElement("input")

  username.type="text"

  username.name="name_"+i

  username.id=i//给这个input赋予id值     

  username.value="动态创建"+i

  inputs.appendChild(username)

  inputs.appendChild(document.createElement("br"))

  //提交信息直接用表单提交,本来内容就是循环在Form里的,你应该懂吧

  }   

 }

</script>

  </head>

  <body>

  动态添加数(输数字):<input type="text" id="js_add_num" name="js_add_num" size="4" onkeyup="addtextarea(this.value)" />

  <form id="inputs" action="像服务端发的请求" method="post">

  </form>

  </body>

</html>

老哥,直接复制过去运行看看是不是你要的效果。嘿嘿

如果不涉及后台的话:你可以在你弹出新增页面(最外层)添加一个div

如:<div id ="content_div"></div>将整个弹出页面包含当中,

然后在页面任意位置,设一个用来保存html的div

<div id="copy_div" style="display:none"></div>

2. 然后在点击确定按钮后,

保存好你新增后的html   $("#copy_div").html($("#content_div").html());

3.在你点击修改的时候保将存号的html内容 取出来 $("#content_div").html($("#copy_div").html())

这样就行啦。