使用 jquery 更改输入的 id

mec*_*b95 0 html javascript jquery

您好,我在表中有一个带有值的 td,当单击按钮时,我已将此 td 更改为文本类型的输入。

现在我想用 td 元素的 id 更改新输入的 id,所以我有一个例外:

input.id 不是函数

这是我的代码js:

$("tr.forc td.TdbeforeForcage").each(function () {
    var html = $(this).html();
    var input = $('<input class="numberforce" style="width:50%" type="text" />');
    var IdTd = $(this).attr("id");
    input.val(html);
    input.id(IdTd); // Here trying to change id of input with id of td 
    $(this).html(input);

    if (html.indexOf('&nbsp;') > -1) {
      var newValue = html.replace('&nbsp;', ' ');
      $(this).find('input.numberforce').val(newValue)
    }
});
Run Code Online (Sandbox Code Playgroud)

Mar*_*cin 5

因为它不是一个函数,而是一个属性,所以您可以像这样更改它:

input.id = IdTd;
Run Code Online (Sandbox Code Playgroud)

使用 jQuery 时:

input.attr('id', IdTd);
Run Code Online (Sandbox Code Playgroud)