首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用jQuery向表体添加行

使用jQuery向表体添加行
EN

Stack Overflow用户
提问于 2012-06-01 21:48:47
回答 6查看 235.3K关注 0票数 79

我正在尝试向表的tbody中添加行。但我在实现这一点上遇到了问题。首先,当html页面中的dropdown发生变化时,调用发生所有事情的函数。我创建了一个包含所有tdtr字符串,其中包含html元素、文本和其他内容。但是当我尝试使用以下命令将生成的行添加到表中时:

代码语言:javascript
复制
$(newRowContent).appendTo("#tblEntAttributes tbody");

我遇到一个错误。该表的名称是tblEntAttributes,我正在尝试将其添加到tbody中。

实际上发生的情况是jQuery无法将tblEntAttributes作为html元素。但我可以使用documemt.getElementById("tblEntAttributes");访问它

有没有什么方法可以通过向表的tbody中添加行来实现这一点?也许做个搭桥手术之类的。

下面是完整的代码:

代码语言:javascript
复制
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)似乎无法工作。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2012-06-04 14:33:38

我从来没有遇到过像这样奇怪的问题!o.O

你知道问题出在哪里吗?$不起作用。我像jQuery("#tblEntAttributes tbody").append(newRowContent);一样用jQuery尝试了同样的代码,它的效果非常棒!

不知道为什么会出现这个奇怪的问题!

票数 17
EN

Stack Overflow用户

发布于 2012-06-01 21:51:45

("#tblEntAttributes tbody")

需要的是

$("#tblEntAttributes tbody")

您没有使用正确的语法选择元素

下面是这两种情况的示例

代码语言:javascript
复制
$(newRowContent).appendTo($("#tblEntAttributes"));

代码语言:javascript
复制
$("#tblEntAttributes tbody").append(newRowContent);

工作http://jsfiddle.net/xW4NZ/

票数 117
EN

Stack Overflow用户

发布于 2014-10-16 19:56:23

使用这个

代码语言:javascript
复制
$("#tblEntAttributes tbody").append(newRowContent);
票数 38
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10851527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档