jquery - 如何查找 ID 与 json 键匹配的表 tr 并填充其他列

标签 jquery ajax json jsp

我是 JQuery 和 Json 的新手,我遇到了一些问题。如果有人能为我提供帮助,我将非常感激。我的问题如下:

在我的 JSP 中,我有 ajax 调用,它将返回 json 对象。我想获取 json 对象中的键,在表中找到其 ID 与键匹配的 tr,并最终使用 json 对象中的值填充该特定行中的其他列。

这是我的 jquery ajax 调用

    $.ajax({
....
....
success:function(data) {    
//Data is a Json Object. The value is for example :{"A":"1","B":"2","C":"3"}    
for(key in data) {
    $("#myTable tr").find(key).find("td").eq(1).html(data[key]); // not working!
}
}
});

,我有以下 html block :

<table id = "myTable">
    <tr id = "A">
      <td>Descriptopn.</td>
 <td>...</td> // I want to set this value to be 1
    </tr>

    <tr id = "B">
 <td>Description</td>
 <td>...</td> // I want to set this value to be 2
    </tr>

     <tr id = "C">
  <td>Description</td>
       <td>...</td> // I want to set this value to be 3
</tr>   
 </table> 

我的问题是上面的语法不起作用。有什么办法可以让我把这件事做好吗?谢谢。

最佳答案

.find()在每个匹配元素中搜索与给定选择器匹配的后代。由于您想要搜索表的后代,而不是行的后代,因此请删除原始选择器中的 tr 。然后,在键前面添加一个 '#' 标记,使其成为有效的 id 选择器,其余部分将按预期工作。

for(var key in data) {
    $("#myTable").find('#'+key).find("td").eq(1).html(data[key]); // working!
}

关于jquery - 如何查找 ID 与 json 键匹配的表 tr 并填充其他列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12706447/

相关文章:

下拉列表值更改时 PHP 更新文本框

php - PHP 和 JavaScript 中的 JSON 和 json_decode

javascript - 如何使用主机提供的速率限制来限制我的 JS API 获取请求?

c# - 在不知道类型的情况下将 Json.NET 对象转换为传统的 .NET 对象

javascript - 如何在 DOM `data` 属性中定义一个 JSON 对象?

javascript - Fancybox,如何在单击下一个或上一个时跳过元素?

javascript - 从 jquery 发送国际电话到 codeigniter Controller 发送空?

java - 如何修复firefox浏览器中的ajax调用,但在chrome浏览器中工作正常

jquery - 在 jQuery 中将图像 SRC 转换为背景图像

javascript - 过滤类(class)中的特定内容