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

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

问题描述

我正在尝试向表中添加行。但是我遇到了这个问题。首先,一切都发生的功能是从html页面改变一个下拉列表。我创建了一个包含所有内容的....包含html元素,文本和其他东西的字符串。但是当我尝试使用以下方式将生成的行添加到表时:

I am trying to add rows to the 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 .... string containing all the 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.

编辑:以下是整个代码:

EDIT : Here's the entire code :

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天全站免登陆