Jquery按行和列Id获取单元格数据

时间:2015-07-30 19:12:00

标签: javascript jquery

我希望按行和列ID获取单元格数据。

以下是示例

var columnId=2;
var rowId=1;

我需要结果7.796

var columnId=3;
var rowId=1;

我需要结果"0.099"

var columnId=6;
var rowId=2;



<table style="width:100%;border-collapse:collapse;float:left;margin-left:10px;margin-bottom: 30px;" border="1" cellspacing="0" cellpadding="0">
   <thead>
  <tr style="background-color:#E5E4E2;color:black">
     <th style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:20%;">Factor</th>
     <th class="factor_value" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;width:2%;">Factor Value</th>
     <th id="2_8" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Inverse Relationship</th>
     <th id="3_9" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Value 2</th>
     <th id="4_10" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Minimum Score</th>
     <th id="5_11" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Midpoint</th>
     <th id="6_12" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Slope</th>
     <th id="7_13" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Transformed Value</th>
  </tr>
   </thead>
   <tbody>
  <tr id="F_4">
     <td style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Operating Margin (Operating Profit / Sales)</td>
     <td id="CH_245" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">7.796</td>
     <td class="fields" fieldtype="2" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">No</td>
     <td class="formula_field fields" formula-coded="(@F_4º@FC_2º==&quot;@Noº&quot;,@CH_244º,@CH_244º*-1)" fieldtype="6" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;"></td>
     <td class="fields" fieldtype="2" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">No</td>
     <td class="fields" fieldtype="1" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">0.099</td>
     <td class="fields" fieldtype="1" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">26.182</td>
     <td class="formula_field fields" formula-coded="1+(1+EXP(-@F_4º@FC_6º*(@F_4º@FC_3º-@F_4º@FC_5º)))" fieldtype="6" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;"></td>
  </tr>
  <tr id="F_6">
     <td style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Cash Ratio (Cash to Current Liabilities)</td>
     <td id="CH_259" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">1.312</td>
     <td class="fields" fieldtype="2" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">No</td>
     <td class="formula_field fields" formula-coded="(@8º====@11_Noº,@C_259º,@C_259º*-1)" fieldtype="6" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;"></td>
     <td class="fields" fieldtype="2" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">No</td>
     <td class="fields" fieldtype="1" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">0.251</td>
     <td class="fields" fieldtype="1" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">11.823</td>
     <td class="formula_field fields" formula-coded="1+(1/EXP(-@12º*(@9º-@11º)))" fieldtype="6" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;"></td>
  </tr>
   </tbody>
</table>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

以下是一种可能的解决方案:

var row=2;//since the head takes a row too
var col1=2;
var col2=6;

console.log($('table tr').eq(row-1).find('td').eq(col1-1).text());
console.log($('table tr').eq(row-1).find('td').eq(col2-1).text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%;border-collapse:collapse;float:left;margin-left:10px;margin-bottom: 30px;" border="1" cellspacing="0" cellpadding="0">
   <thead>
  <tr style="background-color:#E5E4E2;color:black">
     <th style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:20%;">Factor</th>
     <th class="factor_value" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;width:2%;">Factor Value</th>
     <th id="2_8" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Inverse Relationship</th>
     <th id="3_9" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Value 2</th>
     <th id="4_10" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Minimum Score</th>
     <th id="5_11" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Midpoint</th>
     <th id="6_12" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Slope</th>
     <th id="7_13" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Transformed Value</th>
  </tr>
   </thead>
   <tbody>
  <tr id="F_4">
     <td style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Operating Margin (Operating Profit / Sales)</td>
     <td id="CH_245" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">7.796</td>
     <td class="fields" fieldtype="2" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">No</td>
     <td class="formula_field fields" formula-coded="(@F_4º@FC_2º==&quot;@Noº&quot;,@CH_244º,@CH_244º*-1)" fieldtype="6" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;"></td>
     <td class="fields" fieldtype="2" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">No</td>
     <td class="fields" fieldtype="1" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">0.099</td>
     <td class="fields" fieldtype="1" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">26.182</td>
     <td class="formula_field fields" formula-coded="1+(1+EXP(-@F_4º@FC_6º*(@F_4º@FC_3º-@F_4º@FC_5º)))" fieldtype="6" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;"></td>
  </tr>
  <tr id="F_6">
     <td style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">Cash Ratio (Cash to Current Liabilities)</td>
     <td id="CH_259" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">1.312</td>
     <td class="fields" fieldtype="2" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">No</td>
     <td class="formula_field fields" formula-coded="(@8º====@11_Noº,@C_259º,@C_259º*-1)" fieldtype="6" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;"></td>
     <td class="fields" fieldtype="2" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">No</td>
     <td class="fields" fieldtype="1" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">0.251</td>
     <td class="fields" fieldtype="1" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;">11.823</td>
     <td class="formula_field fields" formula-coded="1+(1/EXP(-@12º*(@9º-@11º)))" fieldtype="6" style="border:1px solid gray;text-align:left;color:black;padding:10px;  padding-top: 15px;min-width:3%;"></td>
  </tr>
   </tbody>
</table>

答案 1 :(得分:1)

使用:nth()并使用您的vars构建CSS选择器。查看更多信息here

以下行应该适用于您的代码:

var row = 1;
var column = 2;
$('tbody tr:nth(' + row+ ')').find('td:nth(' + column + ')').text();

请参阅JSFiddle demo

答案 2 :(得分:1)

对于第1行和第2列,请尝试按照选择器

$("table tbody tr:nth-child(1) td:nth-child(2)")

供参考 - http://plnkr.co/edit/LoeHl4dPpl9VRf1ywdwB?p=preview

您现在可以通过传递参数来自定义上述内容。