简体   繁体   English

动态添加表行

[英]Dynamically Adding Table Row

I wonder whether someone could help me please, 我不知道有人能帮我吗

From a tutorial I found online which I've been able to adapt, I've put together the code below, which allows the user to dynamically add additional rows into a table following the insertion of data in the row above. 在网上找到的我可以适应的教程中,我整理了以下代码,该代码使用户可以在将数据插入到上面的行中之后,向表中动态添加其他行。

The problem I'm having is that I can only insert one more row, giving a total of two and I just can't find out why. 我遇到的问题是我只能再插入一行,总共插入两行,而我却找不到原因。 I've been back to the original here to see check that my coding is the same and except for the changes in the field name, table name and the exclusion of the 'Add' button I can't see any differences to cause this problem. 我已经回到原始位置以查看我的编码是否相同,除了字段名,表名的更改以及“添加”按钮的排除之外,我看不到任何导致此问题的差异。

I just wondered whether someone could perhaps take a look at my code please and let me know where I'm going wrong. 我只是想知道是否有人可以看一下我的代码,然后让我知道我要去哪里了。

Javascript 使用Javascript

<script type="text/javascript" language="javascript">
function addRow()
{
    //add a row to the rows collection and get a reference to the newly added row
    var newRow = document.all("addimage").insertRow();

    var oCell = newRow.insertCell();
    oCell.innerHTML = "<input type='radio' name='select'>";

    oCell = newRow.insertCell();
    oCell.innerHTML = "<input type='text' name='title'>";

    oCell = newRow.insertCell();
    oCell.innerHTML = "<input type='file' name='image'>";   
}

//deletes the specified row from the table
function removeRow(src)
{
    /* src refers to the input button that was clicked. 
       to get a reference to the containing <tr> element,
       get the parent of the parent (in this case <tr>)
    */   
    var oRow = src.parentElement.parentElement;  

    //once the row reference is obtained, delete it passing in its rowIndex   
    document.all("addimage").deleteRow(oRow.rowIndex);  

}
</script>

Table Layout 表格布局

<table id="addimage" style="table-layout:auto">
   <tr>
      <td width="20"><input name="select" type="radio" id="select"/></td>
      <td width="144"><input name="title" type="text" id="title"/></td>
      <td width="304"><input name="image" type="file" id="image" onchange="addRow();"/></td>
   </tr>
</table>

The new row that you are adding do not have the onchange property set, it should be: 您要添加的新行没有设置onchange属性,它应该是:

oCell.innerHTML = "<input type='text' name='image' onchange='addRow();'>";   

Also, document.all("addimage") is (as far as i know) bad practice because it is not supported by all browsers. 另外,据我所知,document.all(“ addimage”)是一种不好的做法,因为并非所有浏览器都支持它。 For compatibility you should also add index to insert the row in (-1 for last) and the index for the new cells. 为了兼容性,您还应该添加索引以将行插入(最后为-1),并为新单元格插入索引。 The parameter is required in Firefox and Opera, but optional in Internet Explorer, Chrome and Safari. 该参数在Firefox和Opera中是必需的,而在Internet Explorer,Chrome和Safari中是可选的。 I suggest that you change your code to something like the following: 我建议您将代码更改为以下内容:

function addRow()
{
    //add a row to the rows collection and get a reference to the newly added row
    var newRow = document.getElementById("addimage").insertRow(-1);

    var oCell = newRow.insertCell(0);
    oCell.innerHTML = "<input type='radio' name='select'>";

    oCell = newRow.insertCell(1);
    oCell.innerHTML = "<input type='text' name='title'>";

    oCell = newRow.insertCell(2);
    oCell.innerHTML = "<input type='text' name='image' onchange='addRow();'>";   
}

Seen the actual code. 看到了实际的代码。 In that there is a button, when clicked, adds row to the table. 在其中有一个按钮,当单击时,会将行添加到表中。 In your case you are using onchange event. 在您的情况下,您正在使用onchange事件。 Instead of that use onclick event, as I mentioned in the comment. 就像我在评论中提到的那样,不要使用onclick事件。

Secondly, if you add onclick on every image, you will end up adding extra row on any image click. 其次,如果在每个图像上添加onclick ,最终将在任何图像单击上添加额外的行。 I hope it's okay for you. 希望对你没事。

Suggestion 建议

Instead of using onclick on image use anchor tag which contains your image or use single button as shown in the demo. 代替使用onclick图像,使用包含您的图像的anchor标签或使用演示中所示的单个按钮。 It will look something like this 它看起来像这样

<table id="addimage" style="table-layout:auto">
 <tr>
   <td width="20"><input name="select" type="radio" id="select"/></td>
   <td width="144"><input name="title" type="text" id="title"/></td>
   <td width="304">
      <a href='#' onclick='addRow();'>
        <input name="image" type="file" id="image"/>
      </a>
   </td>
 </tr>
</table>

JavaScript JavaScript的

function addRow()
{
   //add a row to the rows collection and get a reference to the newly added row
   var newRow = document.all("addimage").insertRow();

   var oCell = newRow.insertCell();
   oCell.innerHTML = "<input type='radio' name='select'>";

   oCell = newRow.insertCell();
   oCell.innerHTML = "<input type='text' name='title'>";

   oCell = newRow.insertCell();
   oCell.innerHTML = "<a href='#' onclick='addRow();><input type='file' name='image'>'</a>";   
}

If you want to disable the anchor tag after row is added you can disable it using javascript. 如果要在添加行后禁用锚标记,则可以使用javascript禁用它。 Hope this works. 希望这行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM