HTML 表格“添加行”或“删除行”按钮栏
我需要一种方法来添加或删除 html 表中另一行下方的行。我正在使用 dom,并且可以很容易地添加和删除行,但是当我尝试使用一个动态添加和删除行的按钮列时,问题就出现了。
例如:
| Item Type | Item | Add/Remove Item
====================================================
| Fruit > | Apple | [ Add new ]
----------------------------------------------------
| | Mango | [ Remove ]
----------------------------------------------------
| | Pear | [ Remove ]
----------------------------------------------------
右列中的[添加新]
和[删除]
条目是按钮。单击[Remove]
按钮将删除该行(删除梨或芒果行)。单击[添加新]
按钮将添加一个新项目(新水果)。
如果我为按钮指定了 html 并根据 onClick 添加了新行,我想不出一种方法来跟踪按钮单击哪一行来添加新行。我能想到的最好方法是使用一个数组来跟踪所有项目类型及其所在行,然后将项目的 id 或名称传递到 onClick()
的方法中,但这似乎容易出错且混乱。
为了解决这个问题,我愿意考虑任何解决方案(jquery 插件,javascript 代码,任何东西)。有人可以指出我实现此目标的最佳方法吗?
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 comes 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 accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样?我不知道你要在哪里获取“芒果”和“梨”的值。
http://jsfiddle.net/vPatQ/
用于 html 代码
Like this? I do not know where you are going to take values 'Mango' and 'Pear'..
http://jsfiddle.net/vPatQ/
for html code