我正在尝试向表的tbody
中添加行。但我在实现这一点上遇到了问题。首先,当html页面中的dropdown发生变化时,调用发生所有事情的函数。我创建了一个包含所有td
的tr
字符串,其中包含html元素、文本和其他内容。但是当我尝试使用以下命令将生成的行添加到表中时:
$(newRowContent).appendTo("#tblEntAttributes tbody");
我遇到一个错误。该表的名称是tblEntAttributes
,我正在尝试将其添加到tbody
中。
实际上发生的情况是jQuery无法将tblEntAttributes
作为html元素。但我可以使用documemt.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)
似乎无法工作。
发布于 2012-06-04 14:33:38
我从来没有遇到过像这样奇怪的问题!o.O
你知道问题出在哪里吗?$不起作用。我像jQuery("#tblEntAttributes tbody").append(newRowContent);
一样用jQuery尝试了同样的代码,它的效果非常棒!
不知道为什么会出现这个奇怪的问题!
发布于 2012-06-01 21:51:45
("#tblEntAttributes tbody")
需要的是
$("#tblEntAttributes tbody")
。
您没有使用正确的语法选择元素
下面是这两种情况的示例
$(newRowContent).appendTo($("#tblEntAttributes"));
和
$("#tblEntAttributes tbody").append(newRowContent);
发布于 2014-10-16 19:56:23
使用这个
$("#tblEntAttributes tbody").append(newRowContent);
https://stackoverflow.com/questions/10851527
复制相似问题