我正在尝试向表格的 tbody
添加行,但是我在实现过程中遇到了问题。首先,在一个 HTML 页面的下拉菜单发生更改时调用函数进行操作。我创建了一个包含所有 td
元素、文本和其他内容的 tr
字符串。但是当我尝试使用以下代码将生成的行添加到表格中时:
$(newRowContent).appendTo("#tblEntAttributes tbody");
我遇到了一个错误。表的名称是 tblEntAttributes
,我正在尝试将它添加到 tbody
中。
实际上发生的是 jQuery 无法将 tblEntAttributes
作为 HTML 元素获取。但是我可以使用 document.getElementById("tblEntAttributes");
访问它。
是否有任何办法可以通过将行添加到表的 tbody
中来实现这一点。也许是一个绕过方法或者其他什么。
这是整个代码:
var newRowContent = "<tr><td><input type=\"checkbox\" id=\"" + chkboxId + "\" value=\"" + chkboxValue + "\"></td><td>" + displayName + "</td><td>" + logicalName + "</td><td>" + dataType + "</td><td><input type=\"checkbox\" id=\"chkAllPrimaryAttrs\" name=\"chkAllPrimaryAttrs\" value=\"chkAllPrimaryAttrs\"></td><td><input type=\"checkbox\" id=\"chkAllPrimaryAttrs\" name=\"chkAllPrimaryAttrs\" value=\"chkAllPrimaryAttrs\"></td></tr>";
$("#tblEntAttributes tbody").append(newRowContent);
我忘了提到的一件事是,这段代码所在的函数实际上是一个ajax调用的成功回调函数。我可以使用document.getElementById("tblEntAttributes")
来访问表格,但出于某种原因,$(#tblEntAttributes)
似乎无法工作。