使用jQuery删除<tr>

Anu*_*hav 1 javascript jquery

<table class="table table-bordered table-hover tablesorter">    
   <tbody id="fieldList">    
       <tr id="field_baf1034a_d9d1_a85f_3294_0de635d39c82">
           <td>Description</td>
           <td>Test Description</td>
           <td><a onclick="service.removeRow(field_baf1034a_d9d1_a85f_3294_0de635d39c82);" href="javascript:void(0);"> <i class="fa fa-delete"></i></a>
          </td>
       </tr>
       <tr id="field_85a21c73_da7c_3814_609e_0b743c8f014f">
          <td>Address</td>
          <td>Test Address</td>
          <td><a onclick="service.removeRow(field_85a21c73_da7c_3814_609e_0b743c8f014f);" href="javascript:void(0);"> <i class="fa fa-delete"></i></a></td>
      </tr>
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

Javascript代码:

var service = {
removeRow:function(id){ 
        /* alert(id) == [object HTMLTableRowElement]*/
        $("#"+id).remove();
   }
}
Run Code Online (Sandbox Code Playgroud)

控制台错误:

错误:语法错误,无法识别的表达式:#[object HTMLTableRowElement]

我想删除表格行,请帮忙.

Que*_*tin 10

您传递标识符而不是字符串.

因此,可怕的IE4ism以某种方式将其转换为HTML,导致每个具有id的元素创建具有相同ID的全局JS变量,从而为您提供<tr>元素.

当您"#"+id将HTML Element对象转换为字符串时,即[object HTMLTableRowElement]

在您传递的ID周围加上引号.

service.removeRow('field_baf1034a_d9d1_a85f_3294_0de635d39c82')
Run Code Online (Sandbox Code Playgroud)