我有下面的HTML。

<table border="1" class="myTable">
               <tr>
                  <th class="cname">Component</th>
                  <th class="pname">Properties</th>
                  <th class="sname">lqwasb10</th>
                  <th class="sname">lqwasb11</th>
               </tr>
                     <tr>
                     <td class="cname">InventoryManager</td>
                     <td class="pname">maxConcurrentUpdateRetries</td>
                        <td class="pvalue">1</td>
                        <td class="pvalue">1</td>
                     </tr>
                     <tr>
                     <td class="cname">CatalogTools</td>
                     <td class="pname">queryASAFFabrics</td>
                        <td class="pvalue">skuType="ASAF_FABRIC" AND NOT basicColor IS NULL ORDER BY dynamicAttributes.fabricpriceband, basicColor, dynamicAttributes.fabrictype, dynamicAttributes.asafpattern, dynamicAttributes.asaffabricbrand</td>
                        <td class="pvalue">skuType="ASAF_FABRIC" AND NOT basicColor IS NULL ORDER BY dynamicAttributes.fabricpriceband, basicColor, dynamicAttributes.fabrictype, dynamicAttributes.asafpattern, dynamicAttributes.asaffabricbrand</td>
                     </tr>
                     <tr>
                     <td class="cname">CatalogTools</td>
                     <td class="pname">loggingDebug</td>
                        <td class="pvalue">false</td>
                        <td class="pvalue">false</td>
                     </tr>
</table>


写了下面的jQuery,它不起作用。

 $(document).ready(function(){
           $('.myTable th').each(function(){
              var server = $(this).html();
              if(server === 'lqwasb10'){
                 var b10 = $('.myTable tr td pvalue').text();
                 alert(b10);
              }
           });
       });


我希望b10可以按顺序包含以下值。


1个
skuType =“ ASAF_FABRIC”而不是basicColor是由NULL排序的dynamicAttributes.fabricpriceband,basicColor,dynamicAttributes.fabrictype,dynamicAttributes.asafpattern,dynamicAttributes.asaffabricbrand



上面的代码不返回任何内容。我是一名jQuery新手,如果有人可以为我提供解决方案,那就太好了。

提前谢谢了。

最佳答案

假设您所需的列不一定总是第三列,则可以使用:

var idx;

// Find index of cell with 'lqwasb10'
$('.myTable th').each(function(index) {
  if ($(this).text() === 'lqwasb10') idx = index;
})

// Loop through each cell with the same index
$('.myTable tr').each(function() {
  console.log($(this).find('td:eq('+idx+')').text())
})




var idx;

// Find index of cell with 'lqwasb10'
$('.myTable th').each(function(index) {
  if ($(this).text() === 'lqwasb10') idx = index;
})

// Loop through each cell with the same index
$('.myTable tr').each(function() {
  console.log($(this).find('td:eq('+idx+')').text())
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1" class="myTable">
  <tr>
    <th class="cname">Component</th>
    <th class="pname">Properties</th>
    <th class="sname">lqwasb10</th>
    <th class="sname">lqwasb11</th>
  </tr>
  <tr>
    <td class="cname">InventoryManager</td>
    <td class="pname">maxConcurrentUpdateRetries</td>
    <td class="pvalue">1</td>
    <td class="pvalue">1</td>
  </tr>
  <tr>
    <td class="cname">CatalogTools</td>
    <td class="pname">queryASAFFabrics</td>
    <td class="pvalue">skuType="ASAF_FABRIC" AND NOT basicColor IS NULL ORDER BY dynamicAttributes.fabricpriceband, basicColor, dynamicAttributes.fabrictype, dynamicAttributes.asafpattern, dynamicAttributes.asaffabricbrand</td>
    <td class="pvalue">skuType="ASAF_FABRIC" AND NOT basicColor IS NULL ORDER BY dynamicAttributes.fabricpriceband, basicColor, dynamicAttributes.fabrictype, dynamicAttributes.asafpattern, dynamicAttributes.asaffabricbrand</td>
  </tr>
  <tr>
    <td class="cname">CatalogTools</td>
    <td class="pname">loggingDebug</td>
    <td class="pvalue">false</td>
    <td class="pvalue">false</td>
  </tr>
</table>





请注意,问题中的代码示例中有一个小的错字。第一行正文之后有一个额外的<tr>

10-07 22:00