【问题标题】:how to avoid selection of duplicate html row to clone using jquery如何避免选择重复的 html 行以使用 jquery 进行克隆
【发布时间】:2013-10-24 08:08:23
【问题描述】:

单击表 1 的“td”后,我将该行中的一些单元格附加到另一个表(表 2),而不移动/更改表 1 中的该行。

现在为了避免重复,我将类添加到表 1 中的“td”。 像这样

$(".table1 td:first-child + td").click(function(){

  $("#table2").append('<tr><td>clicked TD value from table 1</td>  more td's</tr>');  
  $(this).addClass('done'); 
}):

现在,当我尝试从 table2 中删除该附加行时...我必须删除“完成”类 这是我添加到 table1 的 'td' 中的。

如何做到这一点?除了添加类之外,还有其他方法(避免重复)吗??

这是fiddle

请帮忙。 谢谢

【问题讨论】:

  • 您能提供更多信息吗? fiddle 可能有助于查看您到底想要做什么并了解所有涉及的约束;)
  • 相反,您必须向两行都传递一些独特的东西,例如 done1,因此在删除 table2 行时,您可以获得它对应的 table1 行
  • @f00bar,刚刚在帖子中添加了小提琴。
  • 当我点击“名称”字段时..没有任何附加内容是第一个问题..从第二个表中删除该行时如何删除我添加的类???
  • 所以基本上你想在单击表选择中的任何名称时将表选择中的行复制到#destiny 表中,但如果你单击的名称已经将它的行复制到#destiny 表中,对吗?

标签: jquery addclass removeclass jquery-append


【解决方案1】:

这是您的代码的深度修改

小提琴HERE

$(function () {
    $("#selections tbody").on('click', function (e) {
        var $td = $(e.target);
        if ($td.prevAll('td').length === 1) {
                var $destiny = $('#destiny'),
                                // Any row in destiny that would be a clone of the current clicked td's row
            // You may want to put an id to make sure it is uniq
                $trDestiny = $('tr[rel="' + $td.text() + '"]', $destiny);
                // ...
            if (!$trDestiny.length) {
                // Insert new row ...
            }
            // else {error handling here}
        }
    });
});

【讨论】:

  • jsfiddle.net/KPe9T/6 我更新了小提琴,我看不懂一些代码...你能详细说明一下吗.???
  • 当然可以!现在检查8th revision...顺便说一句,请将答案设置为考虑我的工作的解决方案;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-22
  • 2015-02-20
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多