日期:2014-05-17  浏览次数:20760 次

用js设置单元格内文本框属性问题。
HTML code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

 <HEAD>

  <TITLE> New Document </TITLE>

  <SCRIPT LANGUAGE="JavaScript">

  <!--

    function addattachfrom() {

        var newnode = document.getElementById('attachSrc').firstChild.cloneNode(true);

        document.getElementById('attachDest').appendChild(newnode);

    }

    function removeattachfrom() {

        var dest = document.getElementById('attachDest');

        dest.childNodes.length >= 1 && dest.lastChild ? dest.removeChild(dest.lastChild) : 0;

    }

  //-->

  </SCRIPT>

 </HEAD>



 <BODY>

 提示:点击[+]可以增加表单,点击[-]可以减少表单

  <table width="90%" align="center" cellpadding="0" cellspacing="0" class="celltable">

      <tbody id="attachSrc">

        <tr><td width="100%">

            <table width="502" border="1" cellpadding="2" cellspacing="2" class="gray" style="border-left:1px solid #DEDEDE;border-right:1px solid #DEDEDE;border-top:1px solid #DEDEDE;border-bottom:1px solid #DEDEDE;">

  <tr>

                    <td width="20%" align="right">名称</td>

                    <td width="40%" align="left">

                        <input type="text" name="GoodsName"/></td>

                    <td width="40%" align="left"><input type="text" name="GoodsName2"/></td>
  </tr>
            </table>

        </td></tr>

      </tbody>

      <tbody id="attachDest"></tbody>

  </table><br />

  <a href="#add" onClick="addattachfrom();"> [+]</a>

  <a href="#remove" onClick="removeattachfrom();">[-]</a>

 </BODY>

</HTML>

用上述代码可以实现插入表格的行,可不知如何设置文本框的name,id,求各位高手解答,谢谢!

------解决方案--------------------
这是一个例子你看看。 
<script type="text/javascript">
var count = 0;
function Add(){
count=count+1;
var File1 = document.getElementById("file1");
var div = document.createElement("div");
var inputTxt = document.createElement("input");
inputTxt.type = "text";
inputTxt.name = "txt"+count;
inputTxt.id = "txt_"+count;
inputTxt.value = "0";
div.appendChild(inputTxt);
File1.appendChild(div);
}
function del(){
alert(count);
if(count>0){
var txt="txt_"+count;
var div=document.getElementById("file1");
var bb=document.getElementById(txt);
bb.parentNode.removeChild(bb);
count=count-1;
}
}
</script>
<body>
<input type="button" name="button" value="增加" onclick="Add();" />
<input type="button" name="button" value="删除" onclick="del();" />
<div id="file1"></div>

设置都有写。
------解决方案--------------------
楼上正解
------解决方案--------------------
inputTxt.type = "text";
inputTxt.name = "txt"+count;
inputTxt.id = "txt_"+count;
inputTxt.value = "0";
 这里已经设了id和name 干嘛还要舍其他的。??
如果真的要那再添加2个test分别是name和id , 你自己手动输入 再添加元素的时候在取。

还有那就是你删除的时候那就不能