在一行上单独运行jQuery函数

在一行上单独运行jQuery函数,jquery,foreach,html-table,Jquery,Foreach,Html Table,我试图重新排序一个html表,其中一个数值位于错误的位置,但其位置不能按常规进行修改 我运行此脚本尝试重新订购td,但是我遇到了尺寸重新订购的问题,可以,但我们将两行的尺寸复制到一行上,例如尺寸将为8,8,10,10,12,12等 我试过运行各种不同的循环,但都没有成功。有人能给我一个提示吗 下面是表格格式,下面是我一直在使用的代码,用于尝试和重新排序for each循环,因为它不适合我 <div id="attributeInputs" class="attribute-inputs j

我试图重新排序一个html表,其中一个数值位于错误的位置,但其位置不能按常规进行修改

我运行此脚本尝试重新订购td,但是我遇到了尺寸重新订购的问题,可以,但我们将两行的尺寸复制到一行上,例如尺寸将为8,8,10,10,12,12等

我试过运行各种不同的循环,但都没有成功。有人能给我一个提示吗

下面是表格格式,下面是我一直在使用的代码,用于尝试和重新排序for each循环,因为它不适合我

<div id="attributeInputs" class="attribute-inputs js-type-grid">
    <table class="js-grid">
    <thead>
        <tr>
            <th class="js-col1"></th> 
            <th class="js-col2" colspan="8"></th>
        </tr>
       </thead>
       <tbody><tr><th rowspan="1"></th>
       <th class="js-rowtitleX">10</th>
       <th class="js-rowtitleX">12</th>
       <th class="js-rowtitleX">14</th>
       <th class="js-rowtitleX">16</th>
       <th class="js-rowtitleX">18</th>
       <th class="js-rowtitleX">20</th>
       <th class="js-rowtitleX">22</th>
       <th class="js-rowtitleX">8</th>
      </tr>
    <tr>
    <th class="js-rowtitleY">Red</th>
   <td class="js-gridBlock js-In_stock" data-attvalue1="10" data-attvalue2="Red" <span class="status">In stock</span></td>
    <td class="js-gridBlock js-In_stock" data-attvalue1="12" data-attvalue2="Red" <span class="status">In stock</span></td>
    <td class="js-gridBlock js-In_stock" data-attvalue1="14" data-attvalue2="Red" <span class="status">In stock</span></td>
    <td class="js-gridBlock js-In_stock" data-attvalue1="16" data-attvalue2="Red" <span class="status">In stock</span></td>
    <td class="js-gridBlock js-In_stock" data-attvalue1="18" data-attvalue2="Red" <span class="status">In stock</span></td>
    <td class="js-gridBlock js-In_stock" data-attvalue1="20" data-attvalue2="Red" <span class="status">In stock</span></td>
    <td class="js-gridBlock js-In_stock" data-attvalue1="22" data-attvalue2="Red" <span class="status">In stock</span></td>
    <td class="js-gridBlock js-In_stock" data-attvalue1="8" data-attvalue2="Red" <span class="status">In stock</span></td>

</tr>
<tr>
<th class="js-rowtitleY">Blue</th>
<td class="js-gridBlock js-In_stock" data-attvalue1="10" data-attvalue2="Blue" <span class="status">In stock</span></td>
<td class="js-gridBlock js-In_stock" data-attvalue1="12" data-attvalue2="Blue" <span class="status">In stock</span></td>
<td class="js-gridBlock js-In_stock" data-attvalue1="14" data-attvalue2="Blue" <span class="status">In stock</span></td>
<td class="js-gridBlock js-In_stock" data-attvalue1="16" data-attvalue2="Blue" <span class="status">In stock</span></td>
<td class="js-gridBlock js-In_stock" data-attvalue1="18" data-attvalue2="Blue" <span class="status">In stock</span></td>
<td class="js-gridBlock js-In_stock" data-attvalue1="20" data-attvalue2="Blue" <span class="status">In stock</span></td>
<td class="js-gridBlock js-In_stock" data-attvalue1="22" data-attvalue2="Blue" <span class="status">In stock</span></td>
<td class="js-gridBlock js-In_stock" data-attvalue1="8" data-attvalue2="Blue" <span class="status">In stock</span></td>
</tr>
</tbody
</table>
</div>

$("#attributeInputs > table > tbody > tr > td").sort(sort_td).appendTo('#attributeInputs > table > tbody > tr:nth-child(n+2)');
    function sort_td(a, b){
                    return ($(b).data('attvalue1')) < ($(a).data('attvalue1')) ? 1 : -1;}

10
12
14
16
18
20
22
8.
红色

问题是,您附加到的选择器中有两个元素,因此这些元素是重复的。要解决这个问题,您应该在
tr
元素上循环,并在这些元素中分别对
td
进行排序。试试这个:

$("#attributeInputs > table > tbody > tr").each(function() {
    $(this).find('td').sort(sort_td).appendTo(this);
})

您是否尝试过使用.each()查看
th
元素并将值存储在数组中,然后对数组进行排序并循环该数组以进行添加?谢谢Rory!我知道这很简单。