在尝试动态删除表行时,得到Uncaught SyntaxError: Unexpected token}

getting Uncaught SyntaxError: Unexpected token } while trying to remove table row dynamically

本文关键字:SyntaxError Uncaught 得到 Unexpected token 动态 删除      更新时间:2023-09-26

我在其他线程中寻找了同样的问题,但无法找到解决方案。
我试图克隆表行并通过更改输入字段的索引将其附加到同一表,我还删除了添加字形图标,并用具有onClick功能的新字形图标替换它,当我试图删除字形图标的点击行时,我得到了这个(Uncaught SyntaxError: Unexpected token})错误。我一遍又一遍地检查我的代码,但没有找到解决方案。
这是我的代码

$(document).ready(function(){

        var glyphRemove="";
            glyphRemove="<span class='glyphicon glyphicon-minus glyph_size' id='remove' onClick='$(this).closest('tr').remove()'  aria-hidden='true'></span>";
                $("#add").click(function() {            
                    var clone= $("#cloneObject").clone();                       
                    clone.removeClass('hide');              
                    clone.prop('disabled',false); 
                    clone.find('#add').remove();
                    clone.find('#glyph').append(glyphRemove);                       
                    clone.appendTo("#shiftsTable");     

                    });
    });   

当我将onClick函数更改为onClick=$this.parent().parent().remove()时,它工作得非常好,并删除包含字形图标的td部分。
我哪里说错了

此处:

glyphRemove="<span class='glyphicon glyphicon-minus glyph_size' id='remove' onClick='$(this).closest('tr').remove()'  aria-hidden='true'></span>";
                $("#add").click(function() {            
                    var clone= $("#cloneObject").clone();                       
                    clone.removeClass('hide');              
                    clone.prop('disabled',false); 
                    clone.find('#add').remove();
                    clone.find('#glyph').append(glyphRemove);                       
                    clone.appendTo("#shiftsTable");     
                }
                ^

                    });

你的代码应该像这样:

$(document).ready(function(){
     var glyphRemove="";
     glyphRemove="<span class='glyphicon glyphicon-minus glyph_size' id='remove' onClick='$(this).closest('tr').remove()'  aria-hidden='true'></span>";
     $("#add").click(function() {            
         var clone= $("#cloneObject").clone();                       
         clone.removeClass('hide');              
         clone.prop('disabled',false); 
         clone.find('#add').remove();
         clone.find('#glyph').append(glyphRemove);                       
         clone.appendTo("#shiftsTable");     
     });
});