使用 jQuery 向表的 tbody 添加行 [英] Adding rows to tbody of a table using jQuery

查看:93
本文介绍了使用 jQuery 向表的 tbody 添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向表的 tbody 添加行.但我在实现这一目标时遇到了问题.首先,在更改 html 页面的下拉列表时调用发生一切的函数.我创建了一个 tr 字符串,其中包含所有 td ,其中包含 html 元素、文本和其他内容.但是当我尝试使用以下方法将生成的行添加到表中时:

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");

我遇到了错误.表的名称是 tblEntAttributes,我正在尝试将它添加到 tbody.

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

实际上发生的事情是 jQuery 无法将 tblEntAttributes 作为 html 元素获取.但我可以使用 documemt.getElementById("tblEntAttributes");

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 添加行来实现这一点.也许是绕过什么的.

Is there any way I can achieve this by adding rows to the tbody of the table. Maybe a bypass or something.

完整代码如下:

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) 似乎不起作用.

One thing I forgot to mention is the function where this code is written is actually the success callback function for a ajax call. I am able to access the table using document.getElementById("tblEntAttributes") but for some reason $(#tblEntAttributes) doesn't seem to work.

推荐答案

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

I have never ever come across such a strange problem like this! o.O

你知道问题出在哪里吗?$ 不起作用.我用 jQuery 尝试了相同的代码,比如 jQuery("#tblEntAttributes tbody").append(newRowContent); 并且它的工作原理很神奇!

Do you know what the problem was? $ isn't working. I tried the same code with jQuery like jQuery("#tblEntAttributes tbody").append(newRowContent); and it works like a charm!

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

No idea why this strange problem occurs!

这篇关于使用 jQuery 向表的 tbody 添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆