【问题标题】:Iterate through a table finding a td by class and changing the text for that td遍历表,按类查找 td 并更改该 td 的文本
【发布时间】:2012-04-22 02:47:05
【问题描述】:

我有一个实体框架生成的表,其中包含我需要更改的 td 值。我需要逐行执行此操作,因为我需要插入一组单选按钮,但每行按钮都需要一个唯一的名称。

基本上,我需要根据 td 中 id 为“Rate”的文本值设置一组按钮的选中值,并在我从中获取值后删除现有文本。

我的桌子是这样的,

<table id="IndexTable">
<tr>
    <th>CoffeeID </th>
    <th>Degree </th>
    <th>Weight </th>
    <th>Profile </th>
    <th>UsageInfo </th>
    <th>Comments </th>
    <th>AmbientTemp </th>
    <th>Rating </th>
    <th>WeightDeducted </th>
    <th>Selected </th>
    <th>ProfileID </th>
    <th>ProfileStart </th>
</tr>
<tr>
    <td>1 </td>
    <td>1 </td>
    <td>3 </td>
    <td>9 </td>
    <td>Filter Brew </td>
    <td>Perfecto!! </td>
    <td>55 </td>
    <td id="Rating">4.25 </td>
    <td>1 </td>
    <td>4 </td>
    <td>34 </td>
    <td>1 </td>

</tr>
<tr>
    <td>3 </td>
    <td>15 </td>
    <td>99 </td>
    <td>87 </td>
    <td>Espresso </td>
    <td>WTF </td>
    <td>99 </td>
    <td id="Rating">4.5 </td>
    <td>5 </td>
    <td>3 </td>
    <td>66 </td>
    <td>4 </td>

</tr>

而我的脚本如下。

$("tr").each(function() { //get all rows in table
var str = $("text[id=Rating]").val(); //assign text with matching id to str ---doesn't work
$("td").each(function() { //get all tds in current row
    var newStr = ("#Rating").text; //assign text in td with id Rating to newStr ---- doesn't work
    alert(newStr); // fires undefined for each td in table? 
    //This is where I want to insert radio buttons and assign the one with the value of the Rating td to checked.
});
});

这只是最新的,我浏览了整个网站,但我所看到的一切都没有涉及从特定行的特定列获取值、更改值以及对每一行重复/表中的列。 我来自 db 背景,这真的很简单,这很令人沮丧。

【问题讨论】:

  • 你打算把单选按钮放在哪里?
  • ID 在 DOM 中应该是唯一的。一个 html 文档中不能有多个 id="Rating"。
  • 我想将 td 中的文本替换为带有单选按钮的评级“指出的类”,并检查与该值匹配的那个。

标签: jquery html entity-framework-4 html-table


【解决方案1】:

您的 javascript 中有一些错误。

  • 使用this获取属于当前tr的tds。
  • "text[id=Rating]" 是一个无效的选择器 - 没有文本标签。
  • #Rating 应该是一个类,因为 ID 必须是唯一的。 (将&lt;td id="Rating"&gt; 更改为&lt;td class="rating"&gt;。
  • .text() 缺少括号。

这是一个更新的示例:

$("tr").each(function() { //get all rows in table
  $("td", this).each(function() { //get all tds in current row
    var newStr = $(".rating").text(); //assign text in td with id Rating to newStr ---- doesn't work
    alert(newStr); // fires undefined for each td in table? 
    //This is where I want to insert radio buttons and assign the one with the value of the Rating td to checked.
  });
});

【讨论】:

  • 谢谢,这行得通,但我选择 Patricia 的只是因为它不会循环太多。我很感激!并感谢您指出 Html 中的唯一 ID 问题!!
【解决方案2】:

首先,您的 html 和 jQuery 有一些问题,就像 SKS 所说的那样,您的 html 文档中不能有多个具有相同 ID 的节点。 ID 必须是唯一的。我建议改用一个类。

您还应该将标题行放在 &lt;thead&gt; 中,将表格正文放在 &lt;tbody&gt; 中

现在,在您的 jQuery 上,$("td") 将找到文档中的所有 td,而不仅仅是您所在行中的那些。

你想使用 $(this).find('td') 或者你可以使用 .children 或者更高级的选择器。有许多不同的正确方法可以获取此信息。

我已经获取了您的代码并进行了如下修改:

$("tbody").find("tr").each(function() { //get all rows in table

    var ratingTdText = $(this).find('td.Rating').text(); 
    //gets the text out of the rating td for this row
    alert(ratingTdText);

});

这适用于稍微修改的 html:

<table id="IndexTable">
<thead>
    <tr>
    <th>CoffeeID </th>
    <th>Degree </th>
    <th>Weight </th>
    <th>Profile </th>
    <th>UsageInfo </th>
    <th>Comments </th>
    <th>AmbientTemp </th>
    <th>Rating </th>
    <th>WeightDeducted </th>
    <th>Selected </th>
    <th>ProfileID </th>
    <th>ProfileStart </th>
</tr>
</thead>
<tbody>
<tr>
    <td>1 </td>
    <td>1 </td>
    <td>3 </td>
    <td>9 </td>
    <td>Filter Brew </td>
    <td>Perfecto!! </td>
    <td>55 </td>
    <td class="Rating">4.25 </td>
    <td>1 </td>
    <td>4 </td>
    <td>34 </td>
    <td>1 </td>

</tr>
<tr>
    <td>3 </td>
    <td>15 </td>
    <td>99 </td>
    <td>87 </td>
    <td>Espresso </td>
    <td>WTF </td>
    <td>99 </td>
    <td class="Rating">4.5 </td>
    <td>5 </td>
    <td>3 </td>
    <td>66 </td>
    <td>4 </td>

</tr>
</tbody>
</table>

希望这可以帮助您朝着正确的方向前进!

哦,我差点忘了!这是一个 jsfiddle 的链接,因此您可以看到它的实际效果:http://jsfiddle.net/fCpc6/

【讨论】:

  • 谢谢帕特里夏!我注意到您为 Roger 回答了类似的问题,但我是 jQuery 新手,不知道如何修改它以供我使用。
  • 我很高兴,jquery 一开始可能有点压倒性。您应该花一些时间查看选择器的文档。它会帮助你很多。
  • 我一直是 Patricia,这只是令人困惑,因为他们的所有示例似乎都是单一案例,而弄清楚如何将它们一起使用是让我感到困惑的地方。现在我只需要弄清楚如何用我的收音机替换单元格中的值。
【解决方案3】:

我想你正在寻找:

var newStr = ("#Rating").text();

或者:

var newStr = ("td[id='Rating']").text();

【讨论】:

    猜你喜欢
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多