简体   繁体   English

尝试使用 jquery 更改表中复制行的 ID 时获取“removeAttr 不是函数”

[英]Getting "removeAttr is not a function" when trying to change id's of copied rows in table with jquery

I am trying to copy rows from one HTML table to another.我正在尝试将行从一个 HTML table复制到另一个。 In my case home address rows to billing address rows.在我的情况下,家庭地址行到帐单地址行。 I need to copy the entire HTML tr elements (with childrens) to ensure number of fields match (I can add more address fields as a user).我需要复制整个 HTML tr元素( tr元素)以确保字段数匹配(我可以作为用户添加更多地址字段)。 so just values isn't enough.所以只有价值观是不够的。

I have successfully cloned the source tr s, successfully updated the class of the tr 's from homeStreet to billingStreet , can paste the new tr rows ok, but how to update the ID's of the individual input fields from input#home-street-1 to input#billing-street-1 .我已经成功克隆了源tr s,成功地将tr的类从homeStreetbillingStreet ,可以粘贴新的tr行,但是如何从input#home-street-1更新各个输入字段的 ID input#billing-street-1 Each tr has one td with the input btw.每个 tr 有一个tdinput btw。

I keep getting stuck and getting different errors.我一直被卡住并收到不同的错误。 Ultimately I'd like to do 1-5 fields (ID's home-street-n with n from 1 to 5) but initially I would settle for 1, ie change table#billing input#home-address-1 to be input#billing-street-2 in the new table#billing HTML table最终我想做 1-5 个字段(ID 的 home-street-n,n 从 1 到 5),但最初我会选择 1,即将table#billing input#home-address-1更改为input#billing-street-2在新table#billing input#billing-street-2 HTML 表

My code is我的代码是

  $('tr.billingStreet').remove();           // Start by removing previous fields.
  const homeStreets = $('tr.homeStreet').clone();
  const billingStreets = homeStreets.removeClass('homeStreet').addClass('billingStreet');
  const billingStreet1 = billingStreets[0]
  billingStreet1.removeAttr('id');          // <-- Error "billingStreet1.removeAttr is not a function"
  billingStreet1.attr('id','billing-street-1');

  // this part works to paste the row
  const beforeRow = $('tr#sameAs');
  beforeRow.after(billingStreet1);

Error:错误:

Uncaught TypeError: billingStreet1.removeAttr is not a function

Adding console.log('billingStreets) gives添加console.log('billingStreets)给出

k.fn.init(2) [tr.billingStreet, tr.billingStreet, prevObject: k.fn.init(2)]
0: tr.billingStreet
1: tr.billingStreet

showing the added class to the new tr's显示添加到新 tr 的类

在此处输入图片说明

Why are you removing attribute if in the next line you overwrite that?如果在下一行中覆盖该属性,为什么要删除该属性?

Remove line:删除行:

 billingStreet1.removeAttr('id');

you don't need that.你不需要那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM