admin管理员组文章数量:1401958
I need a way to add or remove a row below another row in my html table. I'm using the dom, and can add and remove rows easily enough, but the problem es up when I try to have a button column that adds and removes rows dynamically.
For example:
| Item Type | Item | Add/Remove Item
====================================================
| Fruit > | Apple | [ Add new ]
----------------------------------------------------
| | Mango | [ Remove ]
----------------------------------------------------
| | Pear | [ Remove ]
----------------------------------------------------
The [Add new]
and [Remove]
entries in the right column are buttons. Clicking a [Remove]
button would delete the row (remove the pear or mango row). Clicking the [Add new]
button would add a new Item (a new fruit).
If I specified the html for the button and added a new row based on an onClick, I can't think of a way to keep track of which row the button clicked from to add the new one. The best way I can think of is to have an array that keeps track of all the item types and what row they are on, then pass the id or name of the item into the method from onClick()
, but this seems error-prone and messy.
To solve this, I'm willing to consider any solution (jquery plugin, javascript code, anything). Can someone point me to the best way to acplish this?
I need a way to add or remove a row below another row in my html table. I'm using the dom, and can add and remove rows easily enough, but the problem es up when I try to have a button column that adds and removes rows dynamically.
For example:
| Item Type | Item | Add/Remove Item
====================================================
| Fruit > | Apple | [ Add new ]
----------------------------------------------------
| | Mango | [ Remove ]
----------------------------------------------------
| | Pear | [ Remove ]
----------------------------------------------------
The [Add new]
and [Remove]
entries in the right column are buttons. Clicking a [Remove]
button would delete the row (remove the pear or mango row). Clicking the [Add new]
button would add a new Item (a new fruit).
If I specified the html for the button and added a new row based on an onClick, I can't think of a way to keep track of which row the button clicked from to add the new one. The best way I can think of is to have an array that keeps track of all the item types and what row they are on, then pass the id or name of the item into the method from onClick()
, but this seems error-prone and messy.
To solve this, I'm willing to consider any solution (jquery plugin, javascript code, anything). Can someone point me to the best way to acplish this?
Share Improve this question asked Feb 16, 2012 at 0:56 John LeeheyJohn Leehey 22.2k8 gold badges62 silver badges90 bronze badges 1- There's a load of techniques for this, JQuery has a couple of good table plugins; but it might be better to show your work so far. Set up a fiddle here might help: jsfiddle – Russ Clarke Commented Feb 16, 2012 at 0:58
1 Answer
Reset to default 3Like this? I do not know where you are going to take values 'Mango' and 'Pear'.. http://jsfiddle/vPatQ/
$('.AddNew').click(function(){
var row = $(this).closest('tr').clone();
row.find('input').val('');
$(this).closest('tr').after(row);
$('input[type="button"]', row).removeClass('AddNew')
.addClass('RemoveRow').val('Remove item');
});
$('table').on('click', '.RemoveRow', function(){
$(this).closest('tr').remove();
});
for html code
<table>
<tr><td>Item Type</td><td>Item</td><td>Add/Remove Item</td></tr>
<tr><td><input type='text' value='Fruit >'></td>
<td><input type='text' value='Apple'></td>
<td><input type='button' class='AddNew' value='Add new item'></td></tr>
</table>
本文标签: javascriptHTML table quotAdd Rowquot or quotRemove Rowquot button columnStack Overflow
版权声明:本文标题:javascript - HTML table "Add Row" or "Remove Row" button column - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744328000a2600821.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论