使用jQuery获取TR的索引时出错

时间:2015-10-02 20:27:53

标签: javascript jquery html

我的表索引有问题。我使用jQuery index()方法获取索引,但它总是返回1。 这是我的HTML表格:

container.RegisterType<MyType>("name1", ...);
container.RegisterInstance<MyType>("name2", container.Resolve<MyType>("name1"));

这是我的jquery代码:

<table id="tabla_materia" cellspacing="0" cellpadding="0" style="width:500px; margin-top:50px;">
    <tbody>
        <tr>
            <th colspan="6">
                LISTA DE MATERIA PRIMA
            </th>
        </tr>
        <tr>
            <th class="pk"></th>
            <th>Codigo</th>
            <th>Codigo Secundario</th>
            <th>Producto</th>
            <th>Cantidad Requerida</th>
            <th>Cantidad Segun Formula</th>
            <th>Cantidad Total</th>
            <th>Costo Unitario</th>
            <th>Costo Total</th>
        </tr>
        <tr class="ptype">
            <td colspan="10"><strong>3.1.01.1 CARNES - VACUNAS</strong>
                <input type="hidden" class="ptype" value="138">
            </td>
        </tr>
        <tr class="item">
            <td class="pk">1</td>
            <td class="code" rowspan="1">
                <input type="hidden" value="300000563" class="id">
                <input type="hidden" value="31011029" class="code">31011029</td>
            <td class="code2" rowspan="1">32146</td>
            <td class="name" rowspan="1">SAMBARI X KILO</td>
            <td class="requested_quantity"><span class="replaceme">7,000</span>
                <input type="hidden" value="7" class="requested_quantity">
            </td>
            <td class="formula_quantity"><span class="replaceme">0,816</span>
                <input type="hidden" value="0.8163265306122448" class="formula_quantity">
            </td>
            <td class="total_quantity">
                <input type="hidden" value="5.7142857142857135" class="tota_quantity"><span class="replaceme">5,714</span>
            </td>
            <td class="unitary_cost">17.000,00</td>
            <td class="total_cost"><span class="replaceme">97.142,86</span>
                <input type="hidden" class="total_cost" value="97.142,86">
            </td>
        </tr>
        <tr class="ptype">
            <td colspan="10"><strong>3.1.03.1  DESPENSA-ACEITES Y GRASAS</strong>
                <input type="hidden" class="ptype" value="148">
            </td>
        </tr>
        <tr class="item">
            <td class="pk">2</td>
            <td class="code" rowspan="1">
                <input type="hidden" value="1775" class="id">
                <input type="hidden" value="31031005" class="code">31031005</td>
            <td class="code2" rowspan="1">31101</td>
            <td class="name" rowspan="1">ACEITE A GRANEL X LT</td>
            <td class="requested_quantity"><span class="replaceme">7,000</span>
                <input type="hidden" value="7" class="requested_quantity">
            </td>
            <td class="formula_quantity"><span class="replaceme">0,020</span>
                <input type="hidden" value="0.02040816326530612" class="formula_quantity">
            </td>
            <td class="total_quantity">
                <input type="hidden" value="0.14285714285714285" class="tota_quantity"><span class="replaceme">0,143</span>
            </td>
            <td class="unitary_cost">7.300,00</td>
            <td class="total_cost"><span class="replaceme">1.042,86</span>
                <input type="hidden" class="total_cost" value="1.042,86">
            </td>
        </tr>
        <tr class="ptype">
            <td colspan="10"><strong>3.1.03.4 DESPENSA - HUEVOS </strong>
                <input type="hidden" class="ptype" value="151">
            </td>
        </tr>
        <tr class="item">
            <td class="pk">3</td>
            <td class="code" rowspan="1">
                <input type="hidden" value="1718" class="id">
                <input type="hidden" value="31034001" class="code">31034001</td>
            <td class="code2" rowspan="1">31401</td>
            <td class="name" rowspan="1">HUEVOS X UNIDAD</td>
            <td class="requested_quantity"><span class="replaceme">7,000</span>
                <input type="hidden" value="7" class="requested_quantity">
            </td>
            <td class="formula_quantity"><span class="replaceme">6,122</span>
                <input type="hidden" value="6.122448979591836" class="formula_quantity">
            </td>
            <td class="total_quantity">
                <input type="hidden" value="42.857142857142854" class="tota_quantity"><span class="replaceme">42,857</span>
            </td>
            <td class="unitary_cost">641,67</td>
            <td class="total_cost"><span class="replaceme">27.500,00</span>
                <input type="hidden" class="total_cost" value="27.500,00">
            </td>
        </tr>
        <tr class="ptype">
            <td colspan="10"><strong>3.1.03.5 DESPENSA -CONDIMENTOS Y CALDOS</strong>
                <input type="hidden" class="ptype" value="152">
            </td>
        </tr>
        <tr class="item">
            <td class="pk">4</td>
            <td class="code" rowspan="1">
                <input type="hidden" value="300000777" class="id">
                <input type="hidden" value="31035014" class="code">31035014</td>
            <td class="code2" rowspan="1">31517</td>
            <td class="name" rowspan="1">COMINO X KG</td>
            <td class="requested_quantity"><span class="replaceme">7,000</span>
                <input type="hidden" value="7" class="requested_quantity">
            </td>
            <td class="formula_quantity"><span class="replaceme">0,003</span>
                <input type="hidden" value="0.0026122448979591837" class="formula_quantity">
            </td>
            <td class="total_quantity">
                <input type="hidden" value="0.018285714285714287" class="tota_quantity"><span class="replaceme">0,018</span>
            </td>
            <td class="unitary_cost">83.000,00</td>
            <td class="total_cost"><span class="replaceme">1.517,71</span>
                <input type="hidden" class="total_cost" value="1.517,71">
            </td>
        </tr>
        <tr class="item">
            <td class="pk">5</td>
            <td class="code" rowspan="1">
                <input type="hidden" value="1873" class="id">
                <input type="hidden" value="31035040" class="code">31035040</td>
            <td class="code2" rowspan="1">31503</td>
            <td class="name" rowspan="1">PIMIENTA NEGRA EN GRANO X KL</td>
            <td class="requested_quantity"><span class="replaceme">7,000</span>
                <input type="hidden" value="7" class="requested_quantity">
            </td>
            <td class="formula_quantity"><span class="replaceme">0,007</span>
                <input type="hidden" value="0.0065306122448979586" class="formula_quantity">
            </td>
            <td class="total_quantity">
                <input type="hidden" value="0.04571428571428571" class="tota_quantity"><span class="replaceme">0,046</span>
            </td>
            <td class="unitary_cost">69.500,00</td>
            <td class="total_cost"><span class="replaceme">3.177,14</span>
                <input type="hidden" class="total_cost" value="3.177,14">
            </td>
        </tr>
        <tr class="item">
            <td class="pk">6</td>
            <td class="code" rowspan="1">
                <input type="hidden" value="1752" class="id">
                <input type="hidden" value="31035047" class="code">31035047</td>
            <td class="code2" rowspan="1">31501</td>
            <td class="name" rowspan="1">SAL FINA X KL</td>
            <td class="requested_quantity"><span class="replaceme">7,000</span>
                <input type="hidden" value="7" class="requested_quantity">
            </td>
            <td class="formula_quantity"><span class="replaceme">0,065</span>
                <input type="hidden" value="0.06530612244897958" class="formula_quantity">
            </td>
            <td class="total_quantity">
                <input type="hidden" value="0.4571428571428571" class="tota_quantity"><span class="replaceme">0,457</span>
            </td>
            <td class="unitary_cost">1.900,00</td>
            <td class="total_cost"><span class="replaceme">868,57</span>
                <input type="hidden" class="total_cost" value="868,57">
            </td>
        </tr>
        <tr class="item">
            <td class="pk">7</td>
            <td class="code" rowspan="1">
                <input type="hidden" value="2788" class="id">
                <input type="hidden" value="31035048" class="code">31035048</td>
            <td class="code2" rowspan="1">31502</td>
            <td class="name" rowspan="1">SAL GRUESA X KG</td>
            <td class="requested_quantity"><span class="replaceme">7,000</span>
                <input type="hidden" value="7" class="requested_quantity">
            </td>
            <td class="formula_quantity"><span class="replaceme">0,016</span>
                <input type="hidden" value="0.016326530612244896" class="formula_quantity">
            </td>
            <td class="total_quantity">
                <input type="hidden" value="0.11428571428571427" class="tota_quantity"><span class="replaceme">0,114</span>
            </td>
            <td class="unitary_cost">1.020,00</td>
            <td class="total_cost"><span class="replaceme">116,57</span>
                <input type="hidden" class="total_cost" value="116,57">
            </td>
        </tr>
        <tr class="ptype">
            <td colspan="10"><strong>3.1.04.2 VERDURAS</strong>
                <input type="hidden" class="ptype" value="158">
            </td>
        </tr>
        <tr class="item">
            <td class="pk">8</td>
            <td class="code" rowspan="1">
                <input type="hidden" value="1693" class="id">
                <input type="hidden" value="31042004" class="code">31042004</td>
            <td class="code2" rowspan="1">33202</td>
            <td class="name" rowspan="1">AJO X KL</td>
            <td class="requested_quantity"><span class="replaceme">7,000</span>
                <input type="hidden" value="7" class="requested_quantity">
            </td>
            <td class="formula_quantity"><span class="replaceme">0,031</span>
                <input type="hidden" value="0.03061224489795918" class="formula_quantity">
            </td>
            <td class="total_quantity">
                <input type="hidden" value="0.21428571428571425" class="tota_quantity"><span class="replaceme">0,214</span>
            </td>
            <td class="unitary_cost">16.000,00</td>
            <td class="total_cost"><span class="replaceme">3.428,57</span>
                <input type="hidden" class="total_cost" value="3.428,57">
            </td>
        </tr>
        <tr class="item">
            <td class="pk">9</td>
            <td class="code" rowspan="1">
                <input type="hidden" value="1809" class="id">
                <input type="hidden" value="31042018" class="code">31042018</td>
            <td class="code2" rowspan="1">MP0628</td>
            <td class="name" rowspan="1">CEBOLLITA EN HOJA X KL</td>
            <td class="requested_quantity"><span class="replaceme">7,000</span>
                <input type="hidden" value="7" class="requested_quantity">
            </td>
            <td class="formula_quantity"><span class="replaceme">0,122</span>
                <input type="hidden" value="0.12244897959183672" class="formula_quantity">
            </td>
            <td class="total_quantity">
                <input type="hidden" value="0.857142857142857" class="tota_quantity"><span class="replaceme">0,857</span>
            </td>
            <td class="unitary_cost">5.423,73</td>
            <td class="total_cost"><span class="replaceme">4.648,91</span>
                <input type="hidden" class="total_cost" value="4.648,91">
            </td>
        </tr>
        <tr class="item">
            <td class="pk">10</td>
            <td class="code" rowspan="1">
                <input type="hidden" value="1834" class="id">
                <input type="hidden" value="31042025" class="code">31042025</td>
            <td class="code2" rowspan="1">33210</td>
            <td class="name" rowspan="1">LAUREL X KL</td>
            <td class="requested_quantity"><span class="replaceme">7,000</span>
                <input type="hidden" value="7" class="requested_quantity">
            </td>
            <td class="formula_quantity"><span class="replaceme">0,001</span>
                <input type="hidden" value="0.0014285714285714286" class="formula_quantity">
            </td>
            <td class="total_quantity">
                <input type="hidden" value="0.01" class="tota_quantity"><span class="replaceme">0,010</span>
            </td>
            <td class="unitary_cost">30.000,30</td>
            <td class="total_cost"><span class="replaceme">300,00</span>
                <input type="hidden" class="total_cost" value="300,00">
            </td>
        </tr>
        <tr class="item">
            <td class="pk">11</td>
            <td class="code" rowspan="1">
                <input type="hidden" value="1783" class="id">
                <input type="hidden" value="31042064" class="code">31042064</td>
            <td class="code2" rowspan="1">33237</td>
            <td class="name" rowspan="1">APIO X KILO</td>
            <td class="requested_quantity"><span class="replaceme">7,000</span>
                <input type="hidden" value="7" class="requested_quantity">
            </td>
            <td class="formula_quantity"><span class="replaceme">0,027</span>
                <input type="hidden" value="0.026530612244897958" class="formula_quantity">
            </td>
            <td class="total_quantity">
                <input type="hidden" value="0.18571428571428572" class="tota_quantity"><span class="replaceme">0,186</span>
            </td>
            <td class="unitary_cost">4.761,90</td>
            <td class="total_cost"><span class="replaceme">884,35</span>
                <input type="hidden" class="total_cost" value="884,35">
            </td>
        </tr>
    </tbody>
</table>

可能是什么问题? 请帮忙。谢谢。

1 个答案:

答案 0 :(得分:1)

选择器选择td元素并返回td的索引,该索引始终为1,因为它是父td内唯一的tr元素。使用closest('tr')获取tr的索引。

var indiceMateria = $('#tabla_materia tr.ptype input.ptype[value="148"]').closest('tr').index();
//                                                                        ^^^^^^^^^^^^^