I am trying to add rows to the tbody of a table. But I am having issues with achieving that. Firstly, the function where everything takes place is called on change of a dropdown from a html page. I created a tr string containing all the td inside that containing the html elements, text and other stuff. But when I am trying to add that generated row to the table using:

$(newRowContent).appendTo("#tblEntAttributes tbody");

I am encountering an error. The name of the table is tblEntAttributes and I am trying to add it to the tbody.

Actually what's happening is jQuery is unable to get tblEntAttributes as an html element. But I can access it using 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)似乎不起作用.

推荐答案

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

你知道问题出在哪里吗?$没用.我在jQuery上try 了同样的代码,比如jQuery("#tblEntAttributes tbody").append(newRowContent);,它的效果非常好!

No idea why this strange problem occurs!

Jquery相关问答推荐

我在自定义 WordPress 插件中使用 ajax 时收到错误请求

Sheets从多张sheet中导入range匹配数据

jquery如何判断ajax调用的响应类型

用 JS 触发 CSS 悬停

在 jQuery 中构建 html 元素的最清晰方法

使用 JavaScript/jQuery 重定向到 ASP.NET MVC 中的另一个页面

Bootstrap 3 RC 1 中的 typeahead JavaScript 模块在哪里?

将 HTML5 Canvas 转换为要上传的文件?

TypeScript 中是否有this的别名?

jQuery中的正则表达式字段验证

使用 jQuery 发送 JSON 数据

jQuery增量读取AJAX流?

基于垂直滚动的jquery添加/删除类?

在不丢失光标位置的情况下更新输入值

带有回调ajax json的jQuery自动完成

jQuery:如何找到父母的特定子元素?

Jquery live() 与委托()

计算文本宽度

jQuery select2 获取 Select 标签的值?

jquery追加到列表的前面/顶部